fsolve in a for loop

12 ビュー (過去 30 日間)
Mitchell Frechette
Mitchell Frechette 2016 年 10 月 31 日
コメント済み: Star Strider 2016 年 11 月 1 日
I am using fsolve to solve a system of nonlinear equations. The constants in the equations are design variables. I would like to use a for loop to vary one of the design parameters through a range and then save the different outputs (I am only interested in one of the unknowns for this part) into an array. I have attached the code. When I run it, only the first value of the parameter I would like to change is ran through fsolve.
Any help is appreciated. Im sure theres plenty of places my code could improve, but this is my question.

回答 (1 件)

Star Strider
Star Strider 2016 年 11 月 1 日
編集済み: Star Strider 2016 年 11 月 1 日
The colon operator increments by 1 by default, so this line:
for t_CP = .01:.1
will execute only once because after the first iteration the terminating condition, 0.1, is satisfied.
You need to do something like this:
% Solve non-linear equations by iteration
t_CP = .01 : 0.01 : 0.1;
for k1 = 1:length(t_CP)
... DO SOMETHING HERE ...
end
I am not certain what you want to do (I’m actually lost) so I’ve no idea what you’re looping through.
This might be an option:
% Solve non-linear equations by iteration
x0=[250 250 250 250 250 250 250 250 250 250 250 250 250 250 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 250 250 250 250 250 250 250 250 250 250 250 250 250 250]; %guesses for variables
for t_CP = 1:length(x0)
options=optimset('Display','off');
fsol = fsolve(@equation_function_model_2,x0(k1),options)
X(k1) = fsol(1)
end
This is just a guess.
  2 件のコメント
Mitchell Frechette
Mitchell Frechette 2016 年 11 月 1 日
I see now how I misused the colon operator. Thank you for pointing that out. By changing it to...
for t_CP = .01:.01:.1
x0=[250 250 250 250 250 250 250 250 250 250 250 250 250 250 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 250 250 250 250 250 250 250 250 250 250 250 250 250 250]; %guesses for varibles
options=optimset('Display','off');
fsol = fsolve(@equation_function_model_2,x0,options)
end
The fsolve then runs ten times with the t_CP parameter changing ten times, which is what I want. Now, how to i get the ten different values of fsol(1) to be saved into an array?
Star Strider
Star Strider 2016 年 11 月 1 日
See my second code example:
X(k1) = fsol(1);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by