How to call function for every iteration of the for loop

9 次查看(过去 30 天)
Hi I am trying to solve set of nonlinear equations using fsolve function, but for 4 different cases. I defined all the preset variables in a row matrix, but when I try to call the function fsolve it only returns the value by using the 4th case instead of returning values for all 4 cases. How do I call a function for every iteration of the for loop? I am pretty new to MATLAB so any help would be appreciated.
Code:
x0 = [20; 60]; % initial guesses for x(1) and x(2).
options=optimset('Display','iter');
x = fsolve(@myfun,x0,options)
function f=myfun(x0)
x=x0;
A_all = [1.3 1.9 1.8 0.10];
m_all = [80 84 92 25];
j=1;
for Cd= [0.3 1.14 0.29 0.045];
A=A_all(j); %m2
m=m_all(j); %kg
g=9.807; %m/s2
d= 1.225; %kg/m3
f1=sqrt((2*m*g)/(d*A*Cd))*tanh(x(1)* sqrt((g*d*Cd*A)/(2*m)))- x(2);
f2=((2*m)/(d*A*Cd))*(log(abs(cosh(x(1)*sqrt((d*A*Cd*g)/(2*m))))))-1000;
f=[f1 f2];
j=j+1; %go to the next object
end
end

回答(1 个)

Image Analyst
Image Analyst 2020-2-16
Try indexing f to put the f1 and f2 on different rows:
f(j, 1:2) = [f1 f2];
assuming f1 and f2 are scalars, not vectors
  1 个评论
Varun Biradar
Varun Biradar 2020-2-16
I need to get values of x1 and X2 for j=1,2,3,4 but I only get x1 and X2 for j=4. When I call the function. Indexing f is still not giving me the results I'm expecting.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by