Need to solve the system of nonlinear differential equations:
x1p = a1*u2*x1^1.3 + a2*u1 + a3*u3
x2p = (a4*u2 + a5)*x1^1.3 + a6*x2
x3p = (a7*u3 + (a8*u2-a9)*x1)/a10
x1p, x2p & x3p are time derivatives of x1, x2 & x3, i.e. dx1/dt, dx2/dt & dx3/dt.
we have descrete data of x1, x2 & x3 as well as of u1, u2 & u3. We need to solve the problem in order to get the unknown coefficients, a1, a2, …, a10.
Have checked many posts and can say the solution involves ODE45 (or other ODEX) and probably fsolve or fminsearch (Matlab), but have not managed to setup up the problem correctly, guess we don't understand the coding well. Please, suggestions.
you should replace x1p, x2p ,and x3p by using definition of derivative:
x1p = (x1(i+1) - x(i))/ dt , and like this for the others.
then use folowing algorithm (it is not complete):
descrete data of x1, x2 & x3 as well as of u1, u2 & u3
dt = 0.01
myFun = #(a,x1,x2,x3,u1,u2,u3)
[ (x1(i+1) - x1(i))/ dt = a(1)*u2(i)*x1(i)^1.3 + a(2)*u1(i) + a(3)*u3(i);
(x2(i+1) - x2(i))/ dt = (a(4)*u2(i) + a(5)*x1(i)^1.3 + a(6)*x2(i);
(x3(i+1) - x3(i))/ dt = (a(7)*u3(i) + (a(8)*u2(i)-a(9))*x1(i))/a(10) ]
A=[];
a0 = [0; 0; 0 ;0 ;.... ]
for i= 1:1: lenngth(x1)
a=fsolve(#(a)myFun(a,x1,x2,x3,u1,u2,u3),a0,options);
a0 = [ a(1,1) ; a(2,1); a(3,1) ; .......]
A = cat(1,A,a) ;
end
a1 = mean(A(1,:))
a2 = mean(A(2,:))
.
.
a10 = mean(A(10,:))
Related
I have a problem with my MATLAB code that I write to minimize this function with two constraints (one of them is inequality and the other one is equality) with Lagrange Multipliers that use KKT conditions. This the Function :
MIN F = 2*x1^2 + 2*x2^2-6*x1*x2
ineq=x1+2*x2<=3
eq=3*x1+x2=9
I am personally suspect that my if Function(statement) has problem but don't know how to fix that .....
clc
warning off
syms x1 x2 u s lambda
f=2*x1^2+2*x2^2-6*x1*x2;
g1=sqrt(x1^2*x2)+s^2-0.25;
H1=x1*x2^2-0.1;
Lagrange=f+u*g1+lambda*H1;
Grad=gradient(Lagrange);
S=solve(Grad);
S=double([S.x1 S.x2 S.s S.u S.lambda]);M=size(S,1);
for i=M:-1:1
if imag(S(i,3))~=0 || S(i,1)<=0 || S(i,2)<=0
end
end
x1=S(:,1);x2=S(:,2);s=S(:,3);u=S(:,4);lambda=S(:,5);
out=table(x1,x2,s,u,lambda)
x0=[1 0.5 -0.75];
It seems I have a problem because it gives me both write and wrong answers can you help me with that ?
Just eliminate the corresponding row when if condition is satisfied using S(i,:)=[];
According to your formulation
The if condition should look like this
if imag(S(i,3))~=0 || S(i,1)<=0 || S(i,2)<=0
S(i,:)=[];
end
Solution
out =
1×5 table
x1 x2 s u lambda
______ _______ _ ______ _______
0.3393 0.54288 0 3.6349 -2.6402
From my point of view your equality and inequality constraints
formulation are wrong
ineq = x1 + 2*x2 <= 3 -------> g1 = x1 + 2*x2 - 3 + s^2
eq = 3*x1 + x2 = 9 -------> H1 = 3*x1 + x2 - 9
The if condition checks
if u(modifying g1) is less than zero or not
or
if s is real number or not
for i=M:-1:1
if imag(S(i,3))~=0 || S(i,4)<0
S(i,:)=[];
end
end
The entire code is as follow
syms x1 x2 u s lambda
f=2*x1^2+2*x2^2-6*x1*x2;
g1 = x1+2*x2-3 +s^2 ;
H1 = 3*x1+x2-9;
Lagrange=f+u*g1+lambda*H1;
Grad=gradient(Lagrange);
S=solve(Grad)
S=double([S.x1 S.x2 S.s S.u S.lambda]);M=size(S,1)
for i=M:-1:1
if imag(S(i,3))~=0 || S(i,4)<0
S(i,:)=[];
end
end
x1=S(:,1);x2=S(:,2);s=S(:,3);u=S(:,4);lambda=S(:,5);
out=table(x1,x2,s,u,lambda)
Solution
out =
1×5 table
x1 x2 s u lambda
__ __ _ ____ ______
3 0 0 13.2 -8.4
Check your answer using fmincon as follow
% Check your answer using fmincon
% Inequality constraint
%ineq = x1 + 2*x2 <= 3
A = [1, 2];
b = 3;
% Equality constraint
%eq = 3*x1 + x2 = 9
Aeq = [3, 1];
beq = 9;
%F = 2*x1^2 + 2*x2^2-6*x1*x2
F = #(x)2*x(1).^2 + 2*x(2).^2-6*x(1).*x(2);
x0 = [0,0];
[x_minimum, Feval] = fmincon(F, x0, A, b, Aeq, beq);
Solution
x_minimum =
3.0000 -0.0000
Feval =
18.0002
how can I solve this problem with MATLAB in the same way as above? Can I also show it on the chart?
fmin(x1,x2 )=(x1-1)^2+(x2-1)^2
H1=x1+x2-4=0;
H2=x1-x2-2=0
I have a problem with the equation shown below. I want to enter a vector in t2 and find the roots of the equation from different values in t2.
t2=[10:10:100]
syms x
p = x^3 + 3*x - t2;
R = solve(p,x)
R1 = vpa(R)
Easy! Don't use syms and use the general formula:
t2 = [10:10:100];
%p = x^3 + 3*x - t2;
a = 1;
b = 0;
c = 3;
d = -t2;
D0 = b*b - 3*a*c;
D1 = 2*b^3 - 9*a*b*c + 27*a^2*d;
C = ((D1+sqrt(D1.^2 - 4*D0.^3))/2).^(1/3);
C1 = C*1;
C2 = C*(-1-sqrt(3)*1i)/2;
C3 = C*(-1+sqrt(3)*1i)/2;
f = -1/(3*a);
x1 = f*(b + C1 + D0./C1);
x2 = f*(b + C2 + D0./C2);
x3 = f*(b + C3 + D0./C3);
Since b = 0, you can simplify this a bit:
% ... polynomial is the same
D0 = -3*a*c;
D1 = 27*a^2*d;
% ... the different C's are the same
f = -1/(3*a);
x1 = f*(C1 + D0./C1);
x2 = f*(C2 + D0./C2);
x3 = f*(C3 + D0./C3);
Trial>> syms x p
Trial>> EQUS = p == x^3 + 3*x - t2
It is unknown that you want to solve an equation or a system.
Suppose that you want to solve a system.
Trial>> solx = solve(Eqns,x)
But, I do not think you can find roots.
You can solve one equation.
Trial>> solx = solve(Eqns(3),x)
As far as I know, Maple can do this batter.
In general, loops should be avoided, but this is the only solution that hit my brain right now.
t2=[10:10:100];
pp=repmat([1,0,3,0],[10,1]);
pp(:,4)=-t2;
for i=1:10
R(i,:) =roots(pp(i,:))';
end
Assume we have three equations:
eq1 = x1 + (x1 - x2) * t - X == 0;
eq2 = z1 + (z1 - z2) * t - Z == 0;
eq3 = ((X-x1)/a)^2 + ((Z-z1)/b)^2 - 1 == 0;
while six of known variables are:
a = 42 ;
b = 12 ;
x1 = 316190;
z1 = 234070;
x2 = 316190;
z2 = 234070;
So we are looking for three unknown variables that are:
X , Z and t
I wrote two method to solve it. But, since I need to run these code for 5.7 million data, it become really slow.
Method one (using "solve"):
tic
S = solve( eq1 , eq2 , eq3 , X , Z , t ,...
'ReturnConditions', true, 'Real', true);
toc
X = double(S.X(1))
Z = double(S.Z(1))
t = double(S.t(1))
results of method one:
X = 316190;
Z = 234060;
t = -2.9280;
Elapsed time is 0.770429 seconds.
Method two (using "fsolve"):
coeffs = [a,b,x1,x2,z1,z2]; % Known parameters
x0 = [ x2 ; z2 ; 1 ].'; % Initial values for iterations
f_d = #(x0) myfunc(x0,coeffs); % f_d considers x0 as variables
options = optimoptions('fsolve','Display','none');
tic
M = fsolve(f_d,x0,options);
toc
results of method two:
X = 316190; % X = M(1)
Z = 234060; % Z = M(2)
t = -2.9280; % t = M(3)
Elapsed time is 0.014 seconds.
Although, the second method is faster, but it still needs to be improved. Please let me know if you have a better solution for that. Thanks
* extra information:
if you are interested to know what those 3 equations are, the first two are equations of a line in 2D and the third equation is an ellipse equation. I need to find the intersection of the line with the ellipse. Obviously, we have two points as result. But, let's forget about the second answer for simplicity.
My suggestion it's to use the second approce,which it's the recommended by matlab for nonlinear equation system.
Declare a M-function
function Y=mysistem(X)
%X(1) = X
%X(2) = t
%X(3) = Z
a = 42 ;
b = 12 ;
x1 = 316190;
z1 = 234070;
x2 = 316190;
z2 = 234070;
Y(1,1) = x1 + (x1 - x2) * X(2) - X(1);
Y(2,1) = z1 + (z1 - z2) * X(2) - X(3);
Y(3,1) = ((X-x1)/a)^2 + ((Z-z1)/b)^2 - 1;
end
Then for solving use
x0 = [ x2 , z2 , 1 ];
M = fsolve(#mysistem,x0,options);
If you may want to reduce the default precision by changing StepTolerance (default 1e-6).
Also for more increare you may want to use the jacobian matrix for greater efficencies.
For more reference take a look in official documentation:
fsolve Nonlinear Equations with Analytic Jacobian
Basically giving the solver the Jacobian matrix of the system(and special options) you can increase method efficency.
I need to plot parameterized solutions for the following systems of equations using t values from 0 to 0.3 incremented by 0.001 each time:
x′ = 12.3 x − 2.7 y
y′ = 5.7 x − 3.7 y
This is what I have so far, but I'm pretty sure my parametric curves are wrong. I'd be expecting some exponential looking thing, not a lot of straight lines. What am I doing wrong?
A = [ 12.3, -2.7; 5.7, -3.7 ]; %initial matrix
[P D] = eig(A); %finding eigenvalues and eigenvectors
i = [1;4.3]; %initial conditions
H = inv(P)*i; %solving for c1 and c2
t = 0:0.001:0.3;
c1 = 0.2580; %constant
c2 = 4.2761; %constant
B1 = [0.9346;0.3558]; %eigenvector
B2 = [0.1775;0.9841]; %eigenvector
a = 11.2721; %eigenvalue
b = -2.6721; %eigenvalue
x1 = c1*B1*exp(a*t) + c2*B1*exp(b.*t);
x2 = c1*B2*exp(a*t) + c2*B2*exp(b.*t);
plot(x1,x2);
Your problem was calculating x1 and x2. Since B1 and B2 are vectors, doing this:
x1 = c1*B1*exp(a*t) + c2*B1*exp(b.*t);
x2 = c1*B2*exp(a*t) + c2*B2*exp(b.*t);
made x1 and x2 2 by 301 matrices.
The correct result is simpler:
x = c1*B1*exp(a*t) + c2*B2*exp(b*t);
and plotting it gives:
plot(x(1,:),x(2,:));
eaxmple first-order derivative using matlab.if we did for second-order derivative,How does this code change?
% y'=(x+2)/y y(0)=1
% y=sqrt(x^2+4*x+1)
clear all
x(1)=0;
ry(1)=1;
dx=0.1;
for i=1:100
k1=(x(i)+2)/ry(i);
k2=(x(i)+dx/2+2)/(ry(i)+dx/2*k1);
k3=(x(i)+dx/2+2)/(ry(i)+dx/2*k2);
k4=(x(i)+dx+2)/(ry(i)+dx*k3);
ry(i+1)=ry(i)+dx*(k1+2*k2+2*k3+k4)/6;
x(i+1)=x(i)+dx;
end
y=sqrt(x.^2+4*x+1);
plot(x,y,'r-',x,ey,'b-',x,hy,'y-',x,ry,'g-');
maybe this code help you if this is your topic and problem, it is runge kutta code for solving second order derivative problems,this belong to 2 years ago and i wrote it by some help.this problem is defined in the book i said in the last answer.
syms x y1 y2
f=input('input function like:D2y=(-0.1*y2)-x, y1 & y2 subtitute as y & Dy respectively and do not type D2y in front of f input');
f=inline(f);
g=inline(y2);
a1=0;
b1=1;
x1=0;
h=0.5;
n=4;
A=zeros(n,3);
A(1,1)=0;
A(1,2)=a1;
A(1,3)=b1;
for i=1:n
%%%%%%%%%%%%%%k1
F1 = h*(subs(g,[x,y1,y2],[x1,a1,b1]));
F2 = h*(subs(f,[x,y1,y2],[x1,a1,b1]));
W1=F1;
W2=F2;
%%%%%%%%%%%%%%%%k2
a2=a1 + (F1/2);
b2=b1 + (F2/2);
x2= x1 + h/2;
F1 = h*(subs(g,[x,y1,y2],[x2,a2,b2]));
F2 = h*(subs(f,[x,y1,y2],[x2,a2,b2]));
W1= W1+ (2*F1);
W2= W2+ (2*F2);
%%%%%%%%%%%%%%%k3
a3=a1 + (F1/2);
b3=b1 + (F2/2);
x3=x1 + h/2;
F1 = h*(subs(g,[x,y1,y2],[x3,a3,b3]));
F2 = h*(subs(f,[x,y1,y2],[x3,a3,b3]));
W1= W1+ (2*F1);
W2= W2+ (2*F2);
%%%%%%%%%%%%$$k4
a4=a1 + (F1);
b4=b1 + (F2);
x4=x1 + h;
F1 =h*(subs(g,[x,y1,y2],[x4,a4,b4]));
F2 = h*(subs(f,[x,y1,y2],[x4,a4,b4]));
W1= W1+ (F1);
W2= W2+ (F2);
%%%%%%%%%%%%%%%%
W1 = (W1/6);
W2 = (W2/6);
a1=a1+ W1;
b1=b1+ W2;
x1=x1 + h;
A(i+1,1)=x1;
A(i+1,2)=a1;
A(i+1,3)=b1;
end
fprintf('y(2) of runge kutta: %15.14f\n',A(n+1,2));
fprintf('Dy(2)of runge kutta: %15.14f\n',A(n+1,3));