Using fsolve to solve a set of equations with different constants every time
Show older comments
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!
1 Comment
Torsten
on 8 Nov 2018
Call fsolve 1000 times in a loop for your 1000 w1 values.
Accepted Answer
More Answers (1)
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 Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!