How to use fsolve with a variable parameter?

Hello,
I have a problem with 13 unknows and 12 equations. I already solve the problem imposing T21=0.
I would to variate 1 of the unknows (I prefer T21) to generate various solution.
How can i do?
I attach you my function.
Thank you in advance.

 採用された回答

Stephan
Stephan 2019 年 11 月 10 日

2 投票

Define the values fot T21 that you want to calculate for. Then use a for loop and call your function inside the loop as often as you need. To do so define T21 as an input argument to your function. Don't miss to store the results of every run of your loop in another row or column of your corresponding run of the loop.

8 件のコメント

Federico MegaMan
Federico MegaMan 2019 年 11 月 10 日
Thank you for the answer.
I didn't understood where i have to put the for, inside or after the function? And I have to delete T21=x(6)?
Another thing, when you talk about argument of function you mean that i have to write funz(x,T21) instead of funz(x)?
Stephan
Stephan 2019 年 11 月 10 日
編集済み: Stephan 2019 年 11 月 10 日
1. Your function call (= call fsolve) is inside the loop. The function itself stays outside the loop at the end of your code or in a separate file.
2. Yes. Note the information given here to do this successfully.
Federico MegaMan
Federico MegaMan 2019 年 11 月 10 日
Okay, now i have create the loop in another file and it works. There is only 1 problem. I 'm varying T21 from 0 to 5° with step=1° (T21=0:pi/180:5*pi/180) but it calculates for 5 times only a value of the output and in the workspace there is T21=0.0873(5°). Do you know why?
Maybe i didn't understand you when you said: "Don't miss to store the results of every run of your loop in another row or column of your corresponding run of the loop."
Stephan
Stephan 2019 年 11 月 10 日
編集済み: Stephan 2019 年 11 月 10 日
The usual way of getting help here is sharing code instead of pictures from code. I assume your problem could be solved already if you would only give us somethingto copy in our workspaces...
Federico MegaMan
Federico MegaMan 2019 年 11 月 10 日
編集済み: Stephan 2019 年 11 月 11 日
Oh i'm very sorry i'm new and i didn't know that. I'll keep it in mind for the next times, thank you.
Anyway this is my function:
(the file name is funzmia)
function F=funz(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end
and this is the for:
for T21=0:pi/180:5*pi/180
fsolve(@funzmia, [1 2 3 4 5 6 7 8 9 10 11 12])
end
Stephan
Stephan 2019 年 11 月 11 日
T21=0:pi/180:5*pi/180;
sol = zeros(12,numel(T21));
for k = 1:numel(T21)
% Results for x are saved in rows 1...12
% every value of T21 corresponds to 1 column
sol(:,k) = fsolve(@(x)funzmia(x,T21(k)), 1:12);
end
function F=funzmia(x,T21)
T11=x(1);
T12=x(2);
T13=x(3);
T14=x(4);
T15=x(5);
T22=x(6);
T23=x(7);
r23=x(8);
T31=x(9);
T32=x(10);
T33=x(11);
r32=x(12);
%T21=0
F(1)=(5^(0.5))*cos(T11)+cos(T12)+4*cos(T13)+cos(T14)+(5^(0.5))*cos(T15)-8;
F(2)=(5^(0.5))*sin(T11)+sin(T12)+4*sin(T13)+sin(T14)+(5^(0.5))*sin(T15);
F(3)=cos(T21)+3*cos(T22)+r23*cos(T23);
F(4)=sin(T21)+3*sin(T22)+r23*sin(T23);
F(5)=cos(T31)+r32*cos(T32)+3*cos(T33);
F(6)=sin(T31)+r32*sin(T32)+3*sin(T33);
F(7)=T12-T21;
F(8)=T31-T14;
F(9)=T13+T22-pi/2;
F(10)=T33-T22-pi;
F(11)=r23-(1+9+6*sin(T12-T13))^(0.5);
F(12)=r32-(1+9+6*cos(T14-T33))^(0.5);
end
Federico MegaMan
Federico MegaMan 2019 年 11 月 11 日
Okay, it works perfectly now. Thank you very much!
Habeeb Alasadi
Habeeb Alasadi 2020 年 10 月 20 日
編集済み: Habeeb Alasadi 2020 年 10 月 20 日
I do not know how to thatnk you, but thanks Stephan.This is what I have been looking for

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

質問済み:

2019 年 11 月 10 日

編集済み:

2020 年 10 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by