Solving ODE with Simulink in Matlab - matlab

I need to solve this ODE using Simulink and I don't know how to make it.
I only know how to do it using ODE solvers.
y'' - y' - 2y = e^(3x)
y(0)=1, y'(0)=2.
I rewrote the equation obtaining an ODEs:
y' = f(x,y)
y(x0) = y0
y'1 = y2
y2= e^(3*x) + y' + 2y
Using ODE solver.
If someone can help me to solve this using a Simulink Model I would appreciate it.
I know how to solve it in Matlab using ODE solvers as ode23 and ode23s but I don't know how to do it using a Simulink Model.
Thanks in advance

Can you solve it in closed form? Looks doable to me. I advise anyone to have the answer in hand if possible before you start a numerical solution.
Here's what I get. Check me:
y(x) = e^(-x)*(8e^3x + 3e^4x + 1)/12
Wolfram Alpha says this is correct.
(Note: Trouble for large values of x - this response will grow at e^3x rate.)
You need to express this as a set of coupled first order ODEs.
y' = z
z' = z + 2y + e^(3x)
Boundary conditions become:
y(0) = 1
z(0) = 2

You can set up the equation yourself term by term in Simulink and add the initial conditions to the integrators by double clicking and setting the corresponding field.
So a quick implementation looks like
I assumed that your x is a time-like quantity hence I placed a ramp function. Clock etc. would also do.
Alternatively you can form the state space system or the transfer function with explicitly taking the initial conditions into account.

Related

matlab solving numerical differential equations [pic]

I'm trying to solve this numerical differential equations, can someone help?
clc;
clear;
syms A1(z) A2(z)
lamda1 = 1560*(10^-9);
c=3*(10^8);
d_eff=27*(10^-12);
omga1=(2*pi*c)/(lamda1);
omga2=omga1*2;
n=2.2;
k1=(n*omga1)/c;
k2=(n*omga2)/c;
ode1 = diff(A1) == (2*i*(omga1^2)*d_eff*A2*conj(A1)*exp(-i*(2*k1-k2)*z))/(k1*(c^2));
ode2 = diff(A2) == (i*(omga2^2)*d_eff.*(A1.^2).*exp(i*(2*k1-k2)*z))/(k2*(c^2));
odes = [ode1; ode2];
cond1 = A1(0) == 1;
cond2 = A2(0) == 0;
conds = [cond1; cond2];
M = matlabFunction(odes)
sol = ode45(M(1),[0 20],[2 0]);
in this question both ODE are coupled, hence there's only 1 ODE to solve:
1.- use 1st equation to write
A1=f(A2,dA2/dz)
and feed this expression into 2nd equation.
2.- regroup
n1=1j*k1^4/k2^2*1/deff*.25
n2=1j*3*(k1-k2)
now the ODE to solve is
y'=n1/y^2*exp(n2*z)
3.- It can obviously be done in MATLAB, but for this particular ODE in my opinion the Wolfram online ODE Symbolic Solver does a better job.
Input the obtained ODE of previous point into the ODE solver available in this link
https://www.wolframalpha.com/input?i=y%27%27+%2B+y+%3D+0
and solve
4.- The general (symbolic) solutions for A2 are
Note that I used k1 instead of n1 and k2 instead of n2 just in the Wolfram ODE Solver.
Rewording ; the k1 k2 expressions of the general solutions are not the wave numbers k1 k2 of the equations in the question. Just replace accordingly.
5.- Now get A1 using expression in point 2.
6.- I have spotted 2 possible errors in the MATLAB code posted in the question that shouldn't be ignored by the question originator:
In the far right side of the posted MATLAB code, the exponential expressions
6.1.- both show
exp(-i(2*k1-k2)z))/(k1(c^2))
there's this (k1*(c^2)) dividing the exponent wheras in the question none of the exponentials show such denominator their respective exponents.
6.2.- the dk or delta k expression in the exponentials of the question are obviously k2-k1 or k1-k2 , here there may be room for a sign ambiguity, that may shoot a wave solution onto the opposite direction, yet the point here is where
*exp(-1i*(2*k1-k2)*z)
should probably be
*exp(-1i*2*(k1-k2)*z)
or just
exp(-1i*(k1-k2)*z)
6.3.- and yes, in MATLAB (-1)^.5 can be either expressed with 1j or 1i but as written in the MATLAB code made available in the question, since only a chunk of code has been made available, it's fair to assume that no such i=1j has been done.

Plot solutions of ODEs with varying initial conditions

I am trying to solve a system of 2 differential equations with 2 variables. I then want to plot x(t) vs t for various initial conditions, and then do the same for y(t) vs t for various initial conditions. I'm struggling with doing this though, for some reason the MATLAB syntax is not clicking with me at all.
Here is the system of differential equations that I am trying to solve.
function dpdt = lODE(t,p)
dpdt = [-3*p(1) + 2*p(2);
-p(1) + (-1 + -2*p(2))];
end
Any help would be appreciated.
Edit: Here's some more context, and my actual code.
syms y(t) z(t);
eqns = [diff(y,t)==-3*y+2*z, diff(z,t)==y-2*z];
[ySol(t),zSol(t)] = dsolve(eqns);
This outputs the solution as a symfunc, which means I can't use it in PLOT. Again, my goal is to plot one of the solutions versus time.

Solve system of differential equation with embedded non diferential equations, using Octave/Matlab (see picture)

I have the following equation system (click to see picture)
, and would like to solve for X(t), Y(t), Z(t), hopefully using Octave/Matlab, which I'm familiar with, but I would not mind solving it by any other means necessary.
Now, Fsolve is useful for regular equation systems, and Ode45, Lsode are useful for differential equations. But, what about this particular system? Note that the differential equation at the bottom contains not only Y, but, also, both X and Z, and those are dependent on the two non-differential equations above.
To be honest, I'm not quite sure how to approach a basic code to solve this system, and after spending some time thinking, I decided to ask for help. I really appreciate any guidance to solve this problem somewhat efficiently, if it is even possible. Almost any reply would be of some use to me right now.
If you know y, you can solve for x, and this even unconditionally as the second equation in monotonous in x
x = fsolve(#(x) y^2-1413.7*x-1095.2*cos(x)+2169, 0)
Then once you know x, you can solve for z using the known inverse cosine
z = acos(0.20978-cos(x))
This may actually fail to give a result if cos(x) comes close to -1. One can artificially cut out that error, introducing a possibly wrong solution
z = acos(min(1,0.20978-cos(x)))
For simplicity, assemble these operations in a helper function
function [x,z] = solve_xz(y)
x = fsolve(#(x) y^2-1413.7*x-1095.2*cos(x)+2169, 0);
z = acos(min(1,0.20978-cos(x)));
end
Now use that to get the ODE for y
function dy = ode_y(t,y)
[x,z] = solve_xz(y(1));
dy = [ y(2); y(3); 6666.6667*(z-x)-333.3333*y(1)-33.3333*y(2)-5*y(3) ];
end
and apply the ODE solver of your choice. It is quite likely that the system is stiff, so ode45 might not be the best solver.

How to fit equation of the form y=ao+a1logx+a2log(2/x)

I have an equation of the form y=ao+a1logx+a2log(2/x); Is there away to fit this kind of equations?
I tried to use polyfit but finding the coefficients ao,a1 and a2 is difficult for me.
Please Help me.
What toolboxes are available to you?
The easiest way would probably be the cftool. (Type it into your command window) if you have the curve fitting toolbox. But polyfit should do as well.
The main problem I see: Your coefficients are not independent of one another. Because log(2/x) is equal to log(2) - log(x) your equation becomes:
y = ao + a1*log(x) + a2*log(2) - a2*log(x);
which is equivalent to:
y = bo + b1*log(x);
Try that one.

Matlab: Is it possible to numerically solve a system of ode's with a mixture of initial and terminal conditions?

I'm trying to use ode45 to solve a system of ODE's:
[X,Y]= ode45(#sys,[0, T],y0);
where,
function dy = sys(t,y)
dy(1) = f_1(y)
dy(2) = f_2(y)
dy(3) = f_3(y)
end
The problem is that the function ode45 requires that y0 be initial values [y_1(0), y_2(0), y_3(0)], while in my system, I only have the values [y_2(0), y_3(0), y_3(T)] available.
Mathematically, this set of initial/terminal conditions should be enough to pin down the system, but is there any way I can work with that by ode45 or any other functions in MATLAB?
Thanks!
After digging in the Matlab documentation for a little bit, I think the more elegant way is to use the bvp4c function. bvp4c is a function specifically designed to handle boundary value problems like this, as opposed to ode**, which are really for initial value problems only. In fact, there's a whole set of other functions such as deval and bvpinit in Matlab that really facilitate the use of bvp4c. Here's the link to the Matlab documentation.
I'll post a brief (and perhaps a bit contrived) example here:
function [y1, y2, y3] = test(start,T)
solinit = bvpinit(linspace(0,3,10), [1,1,0]);
sol = bvp4c(#odefun,#bvpbc,solinit);
tspan = linspace(start,T,100);
S = deval(sol, tspan);
y1 = S(1,:);
y2 = S(2,:);
y3 = S(3,:);
plot (tspan,y1)
figure
plot (tspan,y2)
figure
plot (tspan,y3)
%% system definition & BVCs
function dydx = odefun(t,y)
dydx(1) = y(1) + y(2) + t;
dydx(2) = 2*y(1) + y(2);
dydx(3) = 3 * y(1) - y(2);
end
function res = bvpbc(y0,yT)
res= [y0(3) yT(2) yT(3)];
end
end
The test function outputs 3 vectors of solutions points for y1, y2 and y3 respectively, and also plots them.
Here are the variable paths plotted by Matlab:
Also, I found this video by professor Jake Blanchard from WMU to be very helpful.
One approach is to use the shooting method to iteratively solve for the unknown initial state y_1(0) such that the desired final state y_3(T) is satisfied.
The iteration proceeds by solving a nonlinear equation F = 0:
F(y_1(0)) = Y_3(T) - y_3(T)
where the uppercase function Y is the solution obtained by integrating the ODE's forward in time from a set of initial conditions. The task is to guess the value of y_1(0) to obtain F = 0.
Since we're now solving a nonlinear equation, all of the usual approaches apply. Specifically you could use either bisection or Newton's method to update your guess for the unknown initial state y_1(0). Note a couple of things:
The ODE's are integrated on [0,T] at each iteration of the nonlinear solve.
There may be multiple solutions for F = 0, depending on the structure of your ODE's.
Newton's method may converge faster than bisection, but may also be numerically unstable unless you can provide a good starting guess for y_1(0).
Using existing MATLAB functions, the bounded scalar nonlinear solver FMINBND might be a good choice as a nonlinear solver.
Hope this helps.