How to loop the fsolve for various sets of parameters
6 views (last 30 days)
Show older comments
I have four different non linear equations to solve for 4 unknowns. The known parameters in the equations vary and i have around 1000 such sets of parameters. How to use fsolve to solve for these various parameter sets and store separate values for the unknowns (say r1,r2,r3,r4)? I am able to solve for 1 set of parameters and I am unable to figure out how to use different sets of paarameters. I tried looping in the fsolve but I am not sure how to proceed. Any help would be great!
Thanks.
%CODE FOR EQUATION SOLVING
%one set of parameters p1,p2,p3 are illustrated here
function F= eq(r)
F(1)= (62624.20783*4*(8.854e-12))-(9*r(4)*r(2)*0.01); %the value 62624.20783 is p1 (has 1000 different values)
F(2)= (46383.18112*r(2)*(0.001)*((2*r(3))+0.15))-(2*r(3)*0.15); % the value 46383.18112 is p2
F(3)= (r(3)*(1-r(4)).^(1.5))- (1.36640009); % the value 1.36640009 is p3
F(4)= r(4)-((4/3)*3.14*((r(2)).^3)*r(1));
end
%code for solving the equations
fun=@eq;
r0=[10000 1E-6 10 2E-15]; %initial guess
options = optimoptions('fsolve','Display','iter','MaxFunEvals',50000, 'MaxIter',1000);
[r,fval,exitflag,output] = fsolve(fun,r0,options);
0 Comments
Accepted Answer
Stephan
on 1 May 2019
Edited: Stephan
on 1 May 2019
Hi,
store the parameters in an array and call fsolve in a loop. In every run of the loop you solve for one set of parameters. The results are also stored in an array, indexed by the loop counter variable too. Dont forget to preallocate.
To do this you will have to read here: https://de.mathworks.com/help/optim/ug/passing-extra-parameters.html
Best regards
Stephan
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!