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.

 Accepted Answer

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 Comments

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 on 10 Nov 2019
Edited: Stephan on 10 Nov 2019
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.
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 on 10 Nov 2019
Edited: Stephan on 10 Nov 2019
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...
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
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
Okay, it works perfectly now. Thank you very much!
I do not know how to thatnk you, but thanks Stephan.This is what I have been looking for

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!