Using fsolve in for loop for find solutions to equation

3 ビュー (過去 30 日間)
Litalo
Litalo 2020 年 11 月 13 日
コメント済み: Stephan 2020 年 11 月 14 日
for example if i have next functions: (which i need solving in the same time in a for loop for different t12)
x2=C*((sin(x2+x1*sinh(t12))))/(tg((x2+x1*cos(t12)))));
x1=C*(sin((x1+x2*sinh(t12)))))/(tg((x1+x2*cos(t12))));
(C- just constant lets say it equal 1)
t12- i change every loop lets say it linspace(-pi, pi)
so i tried to do it like this:
tt12=linspace(-pi, pi); %angle changing
C=1; %just constant
x1_vec=zeros(1,length(tt12)); x2_vec=zeros(1,length(tt12)) % vector in which i collect sollutions
x1=rand; x2=rand; % initial conditions
for i=1:length(tt12)
t12=tt12(i);
F= @(V) [ V(1)-C*sin(V(1)+V(2)*sinh(t12))/(tan(V(1)+V(2)*cos(t12)));
V(2)-C*sin(V(2)+V(1)*sinh(t12))/(tan(V(2)+V(1)*cos(t12)));
];
InitialGuess=[x1;x2];
XY = fsolve(F, InitialGuess);
x1=XY(1);
x2=XY(2);
x1_vec(i)=x1;
x2_vec(i)=x2;
end
but every loop i get the next message which make it really slow: (since every loop it print it!)
what should i do so it will work faster (with no such printing (since i want to use t12 vector of lets say 80000 and then it will be too long if it prints every loop and calculation) ?
Best regards,

採用された回答

Stephan
Stephan 2020 年 11 月 14 日
編集済み: Stephan 2020 年 11 月 14 日
Add this line before you call fsolve
options = optimoptions('fsolve','Display','off');
and then call fsolve with this options:
XY = fsolve(F, InitialGuess, options);
  2 件のコメント
Litalo
Litalo 2020 年 11 月 14 日
編集済み: Litalo 2020 年 11 月 14 日
Hi thanks, but somhow my code stuck (its much longer vector t12 than what i wrote in the example), does it is in general slower to solve an equation in for loop than just for example do simple integration in time?
Best,
Lital
Stephan
Stephan 2020 年 11 月 14 日
You could try to work with parfor.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by