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

21 views (last 30 days)
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!

Accepted Answer

Stephan
Stephan on 8 Nov 2018
Edited: Stephan on 8 Nov 2018
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 Comments
Stephan
Stephan on 8 Nov 2018
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...

Sign in to comment.

More Answers (1)

Catherine Castiblanco
Catherine Castiblanco on 22 Nov 2019
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

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!