Using fsolve to solve a set of equations with different constants every time

조회 수: 7 (최근 30일)
Hi guys, so I have used fsolve to sucessful solve the following four simultaneous non linear equations:
F(1)=x(1)*(i*(w1-d1)+k1)-g*x(3)*x(2)-sqrt(2*k1in)*alpha1in;
F(2)=x(2)*(-i*(w1-d1)+k1)-conj(g)*x(4)*x(1)-sqrt(2*k1in)*conj(alpha1in);
F(3)=x(3)*(i*(w3-d3)+k3)+(g/2)*x(1)^2-sqrt(2*k3in)*alpha3in
F(4)=x(4)*(i*(w3-d3)+k3)+(g/2)*x(2)^2-sqrt(2*k3in)*alpha3in
This is solving for x(i) in the way I want it to. The next step that I cannot seem to make work is that I want to find x(i) for multiple different w1 values. and record each x(i). I know how to record the x(i) for each different w1 if I can get there. I just don't know how to change w1 without changing it manually and recording the x(i). I want to do this for about 1000 w1 values so manually changing is not a great option. Any help would be greatly appreciated!

채택된 답변

Stephan
Stephan 2018년 11월 8일
편집: Stephan 2018년 11월 8일
Hi,
make a loop to vary w1 and pass it to fsolve by using one of the options in passing extra parameters.
An (incomplete) example would perhaps lok like this:
function x_all = main()
%Some code
d1 = 1;
k1 = 2;
g = 9.81;
w3 = 5;
k1in= 5;
alpha1in=10;
alpha3in=10;
x0 = zeros(1,4);
% Values for w1
w1_pool = 1:0.1:10;
% Preallocate result matrix
x_all = zeros(numel(w1_pool,numel(x0)));
% switch dispay off
options = optimoptions('fsolve','Display','off');
% Call fsolve in a loop
for k = 1 : numel(w1_pool)
w1 = w1_pool(k);
x_all(k,:) = fsolve(obj_fun,x0,options);
end
function F = obj_fun(x)
F(1)=x(1)*(1i*(w1-d1)+k1)-g*x(3)*x(2)-sqrt(2*k1in)*alpha1in;
F(2)=x(2)*(-1i*(w1-d1)+k1)-conj(g)*x(4)*x(1)-sqrt(2*k1in)*conj(alpha1in);
F(3)=x(3)*(1i*(w3-d3)+k3)+(g/2)*x(1)^2-sqrt(2*k3in)*alpha3in;
F(4)=x(4)*(1i*(w3-d3)+k3)+(g/2)*x(2)^2-sqrt(2*k3in)*alpha3in;
end
end
Save this function as .m-file named the function Name(main.m in this example). Obtain the result by calling the function like this
result = main
Best regards
Stephan
  댓글 수: 5
Stephan
Stephan 2018년 11월 8일
Thanks@Torsten!
- in addition to Torstens correction, i saw that i made a mistake - use:
x_all = zeros(numel(w1_pool),numel(x0));
instead of
x_all = zeros(numel(w1_pool,numel(x0)));
Also i did not define all the values for example k3, k3in, maybe you missed that part also...
So the corrected version of the code is:
function x_all = main()
%Some code
d1 = 1;
d3 = 1;
k1 = 2;
k3 = 3;
g = 9.81;
w3 = 5;
k1in= 5;
k3in= 4;
alpha1in=10;
alpha3in=10;
x0 = zeros(1,4);
% Values for w1
w1_pool = 1:1:10;
% Preallocate result matrix
x_all = zeros(numel(w1_pool),numel(x0))
% switch dispay off
options = optimoptions('fsolve','Display','off');
% Call fsolve in a loop
for k = 1 : numel(w1_pool)
w1 = w1_pool(k);
x_all(k,:) = fsolve(@obj_fun,x0,options);
end
function F = obj_fun(x)
F(1)=x(1)*(1i*(w1-d1)+k1)-g*x(3)*x(2)-sqrt(2*k1in)*alpha1in;
F(2)=x(2)*(-1i*(w1-d1)+k1)-conj(g)*x(4)*x(1)-sqrt(2*k1in)*conj(alpha1in);
F(3)=x(3)*(1i*(w3-d3)+k3)+(g/2)*x(1)^2-sqrt(2*k3in)*alpha3in;
F(4)=x(4)*(1i*(w3-d3)+k3)+(g/2)*x(2)^2-sqrt(2*k3in)*alpha3in;
end
end
Sorry for the confusion...
Lorcan Conlon
Lorcan Conlon 2018년 11월 8일
Yes guys this is working now. Thanks very much!!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Catherine Castiblanco
Catherine Castiblanco 2019년 11월 22일
Hello, I want to solve my function F(x) by using the command fzero with changing tau values. I took as a base the code of the recommended answer. I am struggling in determining the size of x0 and x_all for my case and I think is because of that matlab returns Error.Any help would be greatly appreciated!
My code is the following :
function x_all=main()
r=0.03172;
s=0.333;
rho=0.001;
theta=0.715;
b=0.6;
x0=[0;1];
%values for tau
tau1_pool = 0.001:0.01:1;
%preallocate result matrix
x_all = zeros(numel(x0),numel(tau1_pool))
% switch display off
%options = optimoptions('fzero','Display','off');
%Call fsolve in a loop
for i = 1 : numel(tau1_pool)
tau1 = tau1_pool(i);
x_all(1,i) = fzero(@obj_fun,x0);
end
function F = obj_fun(x)
F=(r*((1-tau1)*s*(x.^(1-s))*(1-theta))/(r-theta*(1-tau1)*s*x.^(1-s)))- rho -(x.^(s)/b)-r+(tau1/b);
end
end
%call the result by typing result=main

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by