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