How to solve coupled differential equation in matlab using ode45 - matlab

I have two differential equations: da/dt=a(.3/a^3+.7)^1/2 and dτ/dt=1/a. The initial conditions are t=0; a=1 and τ=0, respectively. How can I solve the equations in Matlab? I need to calculate different values of a, t and τ also plot τ vs a. Thanks.

That's quite easy.
First write a function to implement your differential equation and save it with a filename corresponding to the function name:
function dy = my_ode(t,y)
dy(1) = y(1)*(0.3/y(1)^3 + 0.)^(1/2); % a
dy(2) = 1/dy(1); % tau
Then in MATLAB, call the ode45 solver with your function
[t,y] = ode45(#my_ode,[0 10],[1; 0]);
This is the result:

Related

How to simulate a discrete ODE in MATLAB with adaptive step size?

I have a discrete ODE, where u is the input and y is the output and dy is the derivative of y.
dy = #(y, u) 229.888852 - 0.050251*y + 3.116311*u + 0.000075 * y^2
I want to simulate this system with a ODE-solver e.g ODE45. But ODE45 requries a time vector t e.g
tspan = [0 5];
y0 = 0;
[t,y] = ode45(#(t,y) 2*t, tspan, y0);
And I don't have the t in my discrete ODE. I found it difficult to use ODE45 or other ODE-solvers in MATLAB / Octave because they don't handle discrete ODE's.
My question is simple:
How to simulate a discrete ODE in MATLAB with adaptive step size?

How to solve a coupled nonlinear first order differential equation?

I have a problem in the form of 2 equations and known initial conditions. The equations are:
dx/dt = (-a1*sin(y) + a2 + a3*sin(y-x)) / ((dy/dt)*a4*cos(y-x))
dy/dt = (a1*sin(x) -a5 + a6*x + a7*sin(y-x)) / ((dx/dt)*a8*cos(y-x))
where a1 to a8 are variables.
I am trying to plot x vs t and y vs t on MATLAB, but I am not sure how to solve this numerically or analytically. Any help would be appreciated!!
The ODE of your problem cannot be written as dy/dt=f(t,y) nor M(t,y)dy/dt=f(t,y). That means it is a Differential Algebraic Equation which has to be solved numerically in the form:
f(t, y, dy/dt)=0
In matlab this can be done with the command ode15i.
Therefore, the first step is to write the function in a proper way, in this case, one option is:
function f = cp_ode(t,y,yp,a)
f1 = (-a(1)*sin(y(2))+a(2)+a(3)*sin(y(2)-y(1)))/yp(2)*a(4)*cos(y(2)-y(1)) - yp(1);
f2 = (a(1)*sin(y(1))-a(5)+a(6)*y(1)+a(7)*sin(y(2)-y(1)))/yp(1)*a(8)*cos(y(2)-y(1)) - yp(2);
f = [f1 ; f2] ;
end
Then, the initial conditions and integration timespan can be set in order to call ode15i:
tspan = [0 10];
y0 = [1; 1] ;
yp0 = fsolve(#(yp)cp_ode(0,y0,yp,a),<yp0_guess>);
a = ones(1,8) ;
[t,y] = ode15i(#(t,y,yp)cp_ode(t,y,yp,a), tspan, y0, yp0);
It has to be noted that the initial conditions t, y, yp should fulfill the equation f(t, y, yp)=0. Here an fsolve is used to satisfy this condition.
The use of annonymus functions is to define the function with the parameters as inputs and then execute the ODE solver having defined the parameters in the main code.

Solving time-dependent Schrodinger equation using MATLAB ode45

The Schrodinger equation for a time-dependent Hamiltonian is:
I try to implement a solver for the Schrodinger equation for a time-dependent Hamiltonian in ode45. However, because the Hamiltonian $H(t)$ is dependent on time. I do not know how to do interpolation in ode45. Can you give me some hints?
psi0 = [0 1];
H = [1 0;0 1]*cos(t); %this is wrong, I do not know how to implement this and pass it to ode45
hbar = 1;
t = [0:1:100];
[T, psi] = ode45(dpsi, t, psi);
function dpsi = f(t, psi, H, psi0)
dpsi = (1/i)*H*psi;
I also try to come up with a solution of matrix interpolation in
MATLAB: Interpolation that involve a matrix.
H is just an identity matrix in your case, so we can just multiply it with the psi vector to get back the psi vector itself. Then, we bring i*hbar to the right-hand-side of the equation so that the final equation is in a form that ode45 accepts. Finally, we use the following code to solve for psi:
function schrodinger_equation
psi0 = [0;1];
hbar = 1;
t = [0 100];
[T,psi] = ode45(#(t,psi)dpsi(t,psi,hbar),t,psi0);
for i = 1:length(psi0)
figure
plot(T,real(psi(:,i)),T,imag(psi(:,i)))
xlabel('t')
ylabel('Re(\psi) or Im(\psi)')
title(['\psi_0 = ' num2str(psi0(i))])
legend('Re(\psi)','Im(\psi)','Location','best')
end
end
function rhs = dpsi(t,psi,hbar)
rhs = 1/(1i*hbar)*cos(t).*ones(2,1);
end
Note that I have plotted the two components of psi separately and for each such plot, I have also plotted the real and imaginary components separately. Here are the plots for two different values of psi0:

Interpolation within ode45

I have a vector with discrete values that I need to pass into my ODE system and I want to use ode45 command. This vector needs to be interpolated within the solver when I use it. Is there a way this can be done?
I have a coupled system of linear ODEs.
dxdt = f(t)*x + g(t)*y
dydt = g(t)*x + h(t)*y
I have three vectors f(t), g(t) and h(t) as a function of t. I need to pass them into the the solver.
I can code up a Runge-Kutta solver in C or C++. I was suggested that doing it in Matlab would be quicker. Can someone suggest a way to do this?
Sure, you can use interp1. Suppose you have vectors fvec, gvec and tvec containing respectively the values of f, g, and the time points at which these values are taken, you can define your derivative function as:
dxydt = #(t,x) [interp1(tvec, fvec, t) * x(1) + interp1(tvec, gvec, t) * x(2)
interp1(tvec, gvec, t) * x(1) + interp1(tvec, hvec, t) * x(2)];
and use it in ode45:
[T,Y] = ode45(dxydt, tspan, [x0; y0]);

How do you plot nonlinear differential equations in matlab

Dx=y
Dy=-k*y-x^3+9.8*cos(t)
inits=('x(0)=0,y(0)=0')
these are the differential equations that I wanted to plot.
first, I tried to solve the differential equation and then plot the graph.
Dsolve('Dx=y','Dy=-k*y-x^3+9.8*cos(t)', inits)
like this, however, there was no explicit solution for this system.
now i am stuck :(
how can you plot this system without solving the equations?
First define the differential equation you want to solve. It needs to be a function that takes two arguments - the current time t and the current position x, and return a column vector. Instead of x and y, we'll use x(1) and x(2).
k = 1;
f = #(t,x) [x(2); -k * x(2) - x(1)^3 + 9.8 * cos(t)];
Define the timespan you want to solve over, and the initial condition:
tspan = [0, 10];
xinit = [0, 0];
Now solve the equation numerically using ode45:
ode45(f, tspan, xinit)
which results in this plot:
If you want to get the values of the solution at points in time, then just ask for some output arguments:
[t, y] = ode45(f, tspan, xinit);
You can plot the phase portrait x against y by doing
plot(y(:,1), y(:,2)), xlabel('x'), ylabel('y'), grid
which results in the following plot