I have to solve this first-order system ODEs using Matlab.
y' + y - z - u = 0.
z' - y + z - u = 0.
u' - y - z - u = 0.
y(0)=1, z(0)=0, u(0)= 0
The analytical solution of above system of first-order ODEs is:
y = (1/3)e^–x + (1/2)e^–2x + (1/6)e^2x
z = (1/3)e^–x – (1/2)e^–2x + (1/6)e^2x
u = (1/3)e^2x – (1/3)e^–x
I have this code to solve it and plot the numerical solution I obtain using the ODE solvers and the analytical solution I have in the statement.
----------
clear
%options= odeset('RelTol',1e-5);
options= odeset('RelTol',1e-5,'AbsTol',1e-7);
[t23,y23]= ode23('functionB',[0 1],[1 0 0],options);
[t23s,y23s]= ode23s('functionB',[0 1],[1 0 0],options);
figure
ya =((1/3)*exp(-t23) + (1/2)*exp(-2*t23) + (1/6)*exp(2*t23));
za =((1/3)*exp(-t23) - (1/2)*exp(-2*t23) + (1/6)*exp(2*t23));
ua =((1/3)*exp(2*t23) + (1/3)*exp(-t23));
plot(t23,ya,t23,za,t23,ua);
title('\bf{Analytical solutions }')
figure
plot(t23,y23(:,1),'m-.',t23,y23(:,2),'g:', t23,y23(:,3),r);
title('\bf{Numerical solutions using} \it{ode23}')
figure
plot(t23s,y23s(:,1),'m-.',t23s,y23s(:,2),'g:', t23s,y23s(:,3),r);
title('\bf{Numerical solutions using} \it{ode23s}')
%legend('ya','ode23','ode23s',0)
%text(3.4,-1.7,'ya = -2sin(t) - cos(2t) ')
%title('\bf{Analytical and numerical solutions using} \it{ode23, ode23s}')
----------
And also I have the function functionB:
-------
function dy= functionB(t,y)
%-y+z+u
%y-z+u
%y+z+u
dy =[??????];
-------
I don't know how to write the solutions in the code of the function to use it in my code.
If someone can help me with this I will be so grateful.
Thanks in advance.
The MATLAB documentation has some good examples about this.
You must rearrange your ODE like this:
y' = -y + z + u = 0
z' = y + z + u = 0
u' = y + z + u = 0
You then substitude y by y(1), z by y(2) and u by y(3) and now you can write your function like this:
function dy = functionB(t,y)
dy = zeros(3,1);
dy(1) = -y(1)+y(2)+y(3);
dy(2) = y(1)-y(2)+y(3);
dy(3) = y(1)+y(2)+y(3);
end
Related
I am testing MATLAB capabilities in solving equations for a project that I intend to do, so I gave it a test run with something simple, but the results that it gives me are incorrect. I tried to solve two non-linear equations with two unknowns, one of the solutions is correct the other is not.
syms theta d x y
eq1 = d * cos(theta) == x;
eq2 = d * sin(theta) == y;
sol = solve(eq1, eq2, theta, d)
sol.theta
sol.d
The solutions for d are correct, but for theta I get:
-2*atan((x - (x^2 + y^2)^(1/2))/y)
-2*atan((x + (x^2 + y^2)^(1/2))/y)
And the correct answer for theta is simply atan(y/x)
Then when I evaluate these solutions with x = 1, y = 0, I get:
eval(sol.d)
eval(sol.theta)
d = 1, -1
theta = NaN, -3.1416
Solutions for d are correct, but theta in that scenario should be 0.
What am I doing wrong?
EDIT: solving it by hand it looks like this: Divide the y equation by the x equation
y/x = (d * sin(theta)) / (d * cos(theta))
y/x = sin(theta)/cos(theta)
y/x = tan(theta)
theta = atan(y/x)
Even if matlab solves it in some other way and gets a different expression, it should still yield the same final result when I use numbers and it PARTIALLY does.
For x = 1 and y = 0, theta should be 0, => this doesnt work, it gives NaN (explanation bellow)
for x = 1 and y = 1, theta should be 45 degrees => this works
for x = 0 and y = 1 theta should be 90 degrees => this works
And I just checked it again with the 45 and 90 degree values for x and y and it works, but for x = 1 and y = 0 it still gives NaN as one of the answers and that is because it gets a 0/0 from the way it is expressing it
-2*atan((x - (x^2 + y^2)^(1/2))/y)
-2*(1 - (1^2 + 0^2))^(1/2)/0
-2*(1 - 1)^(1/2)/0
0/0
but if its in the form of atan(y/x) the result is
theta = atan(0/1)
theta = atan(0)
theta = 0
Did you mean to solve this:
syms a b theta d real
eq1 = a==d * cos(theta) ;
eq2 = b==d * sin(theta) ;
[sol] = solve([eq1 eq2],[d theta] ,'IgnoreAnalyticConstraints', true,'Real',true,'ReturnConditions',true);
When solving the equations with symbolic x and y, the solver will find a solution with a certain condition, which can be obtained using the argument 'ReturnCondition':
syms x y theta d real
eq1 = d*cos(theta) == x;
eq2 = d*sin(theta) == y;
sol = solve([eq1; eq2],[d theta],'ReturnConditions',true);
This gives the following result for sol
>> sol.d
(x^2 + y^2)^(1/2)
-(x^2 + y^2)^(1/2)
>> sol.theta
2*pi*k - 2*atan((x - (x^2 + y^2)^(1/2))/y)
2*pi*k - 2*atan((x + (x^2 + y^2)^(1/2))/y)
>> sol.parameters
k
>> sol.conditions
y ~= 0 & in(k, 'integer')
y ~= 0 & in(k, 'integer')
As you can see, y = 0 does not fulfill this general solution given by the solver, resulting in your problem for y = 0. You can find solutions for y = 0 by either making y numeric instead of symbolic, or by adding an assumption:
syms x y theta d real
assume(y==0)
sol = solve([eq1; eq2],[d theta],'ReturnConditions',true);
I guess its easier to just set y=0 numeric, for this one condition, since there are already 4 possible solutions and conditions for the three lines above.
Hi I am very new to MATLAB. I was trying to solve these equations to either get an analytical solution or solve them numerically. For the analytical solution, I get the following error:
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
In solve (line 305)
Here is my code:
syms A B Ph Pl
delta = 0.1;
mu = 0.02;
sigma = 0.2;
w = 1;
k = 3;
l = 2;
beta = (0.5 - mu/sigma^2) + ((mu/sigma^2 - 0.5)^2 + 2*delta/sigma^2)^0.5;
alpha = -((0.5 - mu/sigma^2) - ((mu/sigma^2 - 0.5)^2 + 2*delta/sigma^2)^0.5);
eqn1 = (A*(Ph^(-alpha)) + (Ph/delta-mu)) -(B*Ph^beta)-k;
eqn2 = (A*Pl^(-alpha) + Pl/(delta-mu) -w/delta) - B*Pl^beta + l;
eqn3 = -alpha*A*(Ph^(-alpha-1)) + 1/(delta-mu) - (beta*B*Ph^(beta-1));
eqn4 = alpha*A*Pl^(-alpha-1)- (beta*B*Pl^(beta-1));
sol = solve([eqn1==0, eqn2==0, eqn3==0, eqn4==0], [A, B, Ph, Pl]);
Matlab is telling you it can't find an analytic solution, but it is definitely finding numerical solutions when I run it, however, they're all complex. Type:
sol.A
in your command window to see what A looks like, same with B, Ph and Pl.
I passed the following nonlinear system to Matlab:
2(x−p1)+2(xy−p3)y = 0
2(y−p2)+2(xy−p3)x = 0
and used syms to find solution for x and y symbolically but I got:
sol.x
ans =
(p1^3 + p3*p1^2*z1 + p1*z1^4 - 1.0*p2*p1*z1^3 + p1*z1^2 - 1.0*p2*p1*z1 + p3*z1^3 - 1.0*p2*p3*z1^2 + p3*z1 - 1.0*p2*p3)/(p1^2 + p3^2)
and
sol.y
ans =
z1
where z1 = RootOf(z^5 - p2*z^4 + 2*z^3 - z^2*(2*p2 - p1*p3) + z*(p1^2 - p3^2 + 1) - p1*p3 - p2, z)
I dont understand where z come from? what is z?
Your y solution is expressed in terms of roots of a polynomial in z that depends on your equation's parameters also.
To show why is difficult to answer your question in the present form, please allow me to rephrase it: the numbers I'm looking for are the roots of an equation f(z) = 0; now, where z comes from? :-)
I need to write a for loop in matlab to solve a derivative using the forward difference method. The function to derive is 10+15x+20x^2 from 0 to 10 using steps of 0.25. I have tried using
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
y(1) = 45; size(x)
for i=2:47,
y(i) = y(i-1) + h*(15+40*x);
end
I'd do like this, as a start,
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
diff(y)./diff(x)
or, as alternative,
syms x;
y = 20.*x.^2 + 15.*x + 10;
dy = diff(y,1);
h=.25;
xx=[0:h:10];
res = subs(dy,xx);
alright well I have the follow function
y=sin(x)^2 + [(10+2x+x^2) / (5+2x^2)]
i need to plot it on the interval y = -2 to y = 2 so how would I set that up?
I did this in matlab
>> y = sin(x).^2 + (10 + 2*x + x.^2)/(5+2*x.^2)
>> x = -2:0.01:2;
is that a correct setup? Or have I done something wrong
You need to declare a variable before you use it. In this case, x doesn't depend on y, so declare it first. In addition, there is a ./ operator missing.
x = -2:0.01:2;
y=sin(x).^2 + (10+2*x+x.^2) ./ (5+2*x.^2);
plot(x,y)
f = #(x) sin(x)^2 + [(10+2*x+x^2) / (5+2*x^2)];
ezplot(f)