Solve a system of equations

1 Ansicht (letzte 30 Tage)
Rustem Devletov
Rustem Devletov am 10 Mai 2019
Bearbeitet: Rustem Devletov am 10 Mai 2019
Can anyone help me to write a code for solving the following system of equations?
w0=10; k1=3; k2=6; V1=20; V2=30;
x21=(w0+x13)/(w0+x13+k1*V1);
x22=(w0+x13);
x31=x22*x21/(x22+k2*V2);
x32=x22;
x13=x31*x32;
Suppose that x13 has an initial value (any number, let's say it is 1); Then we define what x21, x22, x31, 32 will be equal to and new value of x13*n;
If |x13*n-x|>=e (where e is a small number), then a new value of x13 should be x13*n, and we should solve that system until |x13*n-x13|<e;
Below is my idea, but I know it may be stupid but don't judge too harsh because I'm new to matlab and don't know how to do that.
w0=10; k1=3; k2=6; V1=20; V2=30; x13=1; e=0.00001; n=0.1
while abs(x13*n-x13)>=e
x21=(w0+x13)/(w0+x13+k1*V1);
x22=w0+x13;
x31=x22*x21/(x22+k2*V2);
x32=x22;
x13=x31*x32;
x13=x13*n;
end
It never stops counting
  2 Kommentare
darova
darova am 10 Mai 2019
Not allowed to use fsolve()?
Rustem Devletov
Rustem Devletov am 10 Mai 2019
We can use anything. If you can, show me please how to do that

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 10 Mai 2019
x22 - w0 =
x13 =
x31 * x32 =
x22 * x21 / (x22+k2*V2) * x22 =
x22 * (x22/(x22+k1*V1)) / (x22+k2*V2) * x22
This gives a quadratic equation in x22. Solve it.
Once you have x22, x13 = x22 - w0.
  8 Kommentare
Torsten
Torsten am 10 Mai 2019
Then try your fsolve-code. fsolve uses Newton's (iterative) method. But it might converge to the wrong solution because the quadratic equation in x22 usually has two solutions.
Rustem Devletov
Rustem Devletov am 10 Mai 2019
Bearbeitet: Rustem Devletov am 10 Mai 2019
Can you check it out please?
file 1
global w0 k1 k2 x32
w0=10; k1=3; k2=6; V1=20; V2=30 x13=1; e=0.00001;
k=x13;
x21=(w0+x13)/(w0+x13+k1*V1);
x22=w0+x13;
x31=x22*x21/(x22+k2*V2);
x32=x22;
x13=x31*x32;
while abs(x13-k)>=e
k=x13;
x21=(w0+x13)/(w0+x13+k1*V1);
x22=w0+x13;
x31=x22*x21/(x22+k2*V2);
x32=x22;
x13=x31*x32
end
file 2
function [Q]= myfun(x)
global x32
alpha1=0.5;
alpha2=0.5;
V1=x(1);
V2=x(2);
C1=10; %cost 1
C2=15; %cost 2
C3=24;%cost 3
Q=C1*V1^alpha1+C2*V2*alpha2+C3*x32;
end
file 3 (solution)
clear all
clc
fun = @myfun;
x = [20, 30];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [10, 20];
ub = [30, 40];
nonlcon = [];
options = optimoptions('fmincon');
Cany
[x,fval,exitflag,output] = fmincon(fun,x,A,b,Aeq,beq,lb,ub,nonlcon,options)
I get the following error
Error using fmincon (line 612)
Supplied objective function must return a scalar value.
Error in solution (line 14)
[x,fval,exitflag,output] = fmincon(fun,x,A,b,Aeq,beq,lb,ub,nonlcon,options)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Systems of Nonlinear Equations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by