Solving a system with for loop

2 次查看(过去 30 天)
Jolijn  Pruijmboom
Jolijn Pruijmboom 2021-5-25
评论: Torsten 2021-5-25
I would like to solve a system of equations using a for loop and plotting the data.
This is my system
function f = Thesissysteem(x,parameters)
thethaA1= parameters(1);
thethaA2= parameters(2);
thethaN1= parameters(3);
thethaN2= parameters(4);
ebar = parameters(5);
alpha1 = parameters(6);
alpha2 = parameters(7);
sigma = parameters(8);
B = parameters(9);
cap = parameters(10);
tax = parameters(11)
f(1)= x(1) - B * x(11).^thethaA1;
f(2)= x(2) - B * x(12).^thethaA2;
f(3)= x(3) - B * x(13).^thethaN1;
f(4)= x(4) - B * x(14).^thethaN2;
f(5)= 1 * (x(1)-x(5))+ x(17) * (x(2)-x(6))-x(18) * x(11)-x(19) * x(12);
f(6)= 1 * (x(3)-x(7))+ x(17) * (x(4)-x(8))-x(18) * x(13)-x(19) * x(14);
%f(7)= 1 * thethaA1 * B * x(11).^(thethaA1-1) - x(18);
f(7)= x(11) - cap
f(8)= x(17) * thethaA2 * B * x(12).^(thethaA2-1) - x(19);
f(9)= 1 * thethaN1 * B * x(13).^(thethaN1-1) - x(18);
f(10)= x(17) * thethaN2 * B * x(14).^(thethaN2-1) - x(19);
f(11)= (alpha1 / alpha2) * (x(5) / x(6)).^ (-1 / sigma) - (1 / x(17));
%f(11)= (alpha1 / alpha2) * (x(5) / x(6)).^ (-1 / sigma) - (1 + tax / x(17))
f(12)= (alpha1 / alpha2) * (x(7) / x(8)).^ (-1 / sigma) - (1 / x(17));
f(13)= (alpha1 / alpha2) * (x(9) / x(10)).^ (-1 / sigma) - (1 / x(17));
f(14)= x(1) + x(3) - x(5) - x(7) - x(9);
f(15)= x(2) + x(4) - x(6) - x(8) - x(10);
f(16)= x(15) - x(11) - x(13);
f(17)= x(16) - x(12) - x(14);
f(18)= ebar - x(15) - x(16);
f(19)= x(18) - x(19);
With the following values for the parameters
thethaA1= 0.05;
thethaA2= 0.05;
thethaN1= 0.05;
thethaN2= 0.05;
ebar = 1;
alpha1 = 1.5631;
alpha2 = 1;
sigma = 0.5;
B = 1.191;
cap = 0;
tax = 0;
I want to find values for x(11) with different values for sigma (0<sigma<1) with steps of 0.1.
thethaA1= 0.05;
thethaA2= 0.05;
thethaN1= 0.05;
thethaN2= 0.05;
ebar = 1;
alpha1 = 1.5631;
alpha2 = 1;
sigma = 0.3;
B = 1.191;
cap = 0;
tax = 0;
parameters = [thethaA1 thethaA2 thethaN1 thethaN2 ebar alpha1 alpha2 sigma B cap tax];
for i = 0:0.1:1
parameters = [thethaA1 thethaA2 thethaN1 thethaN2 ebar alpha1 alpha2 sigma B cap tax];
[t1, x(11)] = Thesissysteem(parameters, i);
end
Is this the correct way to create such a loop? I get the following error:
Error using Thesissysteem
Too many output arguments.
Error in untitled2 (line 20)
[t1, x(11)] = Thesissysteem(parameters, i);
Can someone help me please?
  5 个评论
Jolijn  Pruijmboom
Jolijn Pruijmboom 2021-5-25
clear;
clc;
thethaA1= 0.05;
thethaA2= 0.05;
thethaN1= 0.05;
thethaN2= 0.05;
ebar = 1;
alpha1 = 1.5631;
alpha2 = 1;
sigma = 0.5;
B = 1.191;
cap = 0;
tax = 0;
parameters = [thethaA1 thethaA2 thethaN1 thethaN2 ebar alpha1 alpha2 sigma B cap tax];
guess = [ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
[solution, fval, eflag, out] = fsolve(@(x) Thesissysteem(x,parameters),(guess),...
optimset('TolX',1.0e-8,'TolFun',1.0e-8,'MaxFunEvals',100000,'MaxIter', 100000));
I use this to solve the system!
Torsten
Torsten 2021-5-25
Then call fsolve in a loop:
sigma_array = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9];
for i=1:numel(sigma_array)
sigma = sigma_array(i);
parameters(8) = sigma;
% your call to fsolve
solution_array(i,:) = solution; % maybe to be changed to solution_array(:,i) = solution if solution is a column vector
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by