How can i solve function with an dependent variable?
3 次查看(过去 30 天)
显示 更早的评论
There is a g(u) function and u variable's values are dependent to g(u) function, its derivative and smaller u.
I do not know how to write this as a matlab code.
0 个评论
采纳的回答
Rik
2021-6-10
Below I show the structure of a possible solution. The rest is up to you. Feel free to comment if you have a specific question.
g=@(mu,B1,B2,B3,a1,a2,a3) B1./a1;
g_=@(mu,B1,B2,B3,a1,a2,a3) 2./(a1-mu);
mu_k=0;
while true
mu_kp1=mu_k-g(mu_k,B1,B2,B3,a1,a2,a3)./g_(mu_k,B1,B2,B3,a1,a2,a3);
if mu_kp1/mu_k < 1
break
end
end
6 个评论
Rik
2021-6-11
After a minor edit, the code runs fine. And you only need pre-allocation if you expect many iterations, in which case I would suggest avoiding storing all intermediate values, unless the explicit goal is to find the trend in iterations of mu for different values of your betas and alphas.
%generate random values, but make sure they are the same every run
rng(0);[B1,al1,B2,al2,B3,al3]=deal(rand);
g = @(u) (B1./(al1 - u)).^2 + (B2./(al2 - u)).^2 + (B3./(al3 - u)).^2 -1;
derivg = @(u) 2/(al1-u).*(B1./(al1 - u)).^2 + 2/(al2-u).*(B2./(al2 - u)).^2 + 2/(al3-u).*(B3./(al3 - u)).^2 ;
u=0; k=1;
while true
u(k+1)= u(k)-g(u(k))./derivg(u(k));
k=k+1;
if abs((u(k)- u(k-1))./u(k)) < 0.000001
% ^ ^
% you forgot these
break
end
end
disp(u)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!