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.
采纳的回答
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 个评论
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)?
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.
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."
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!
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 Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
