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
Related
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?
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:
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:
I need some help with finding solution to Cauchy problem in Matlab.
The problem:
y''+10xy = 0, y(0) = 7, y '(0) = 3
Also I need to plot the graph.
I wrote some code but, I'm not sure whether it's correct or not. Particularly in function section.
Can somebody check it? If it's not correct, where I made a mistake?
Here is separate function in other .m file:
function dydx = funpr12(x,y)
dydx = y(2)+10*x*y
end
Main:
%% Cauchy problem
clear all, clc
xint = [0,5]; % interval
y0 = [7;3]; % initial conditions
% numerical solution using ode45
sol = ode45(#funpr12,xint,y0);
xx = [0:0.01:5]; % vector of x values
y = deval(sol,xx); % vector of y values
plot(xx,y(1,:),'r', 'LineWidth',3)
legend('y1(x)')
xlabel('x')
ylabel('y(x)')
I get this graph:
ode45 and its related ilk are only designed to solve first-order differential equations which are of the form y' = .... You need to do a bit of work if you want to solve second-order differential questions.
Specifically, you'll have to represent your problem as a system of first-order differential equations. You currently have the following ODE:
y'' + 10xy = 0, y(0) = 7, y'(0) = 3
If we rearrange this to solve for y'', we get:
y'' = -10xy, y(0) = 7, y'(0) = 3
Next, you'll want to use two variables... call it y1 and y2, such that:
y1 = y
y2 = y'
The way you have built your code for ode45, the initial conditions that you specified are exactly this - the guess using y and its first-order guess y'.
Taking the derivative of each side gives:
y1' = y'
y2' = y''
Now, doing some final substitutions we get this final system of first-order differential equations:
y1' = y2
y2' = -10*x*y1
If you're having trouble seeing this, simply remember that y1 = y, y2 = y' and finally y2' = y'' = -10*x*y = -10*x*y1. Therefore, you now need to build your function so that it looks like this:
function dydx = funpr12(x,y)
y1 = y(2);
y2 = -10*x*y(1);
dydx = [y1 y2];
end
Remember that the vector y is a two element vector which represents the value of y and the value of y' respectively at each time point specified at x. I would also argue that making this an anonymous function is cleaner. It requires less code:
funpr12 = #(x,y) [y(2); -10*x*y(1)];
Now go ahead and solve it (using your code):
%%// Cauchy problem
clear all, clc
funpr12 = #(x,y) [y(2); -10*x*y(1)]; %// Change
xint = [0,5]; % interval
y0 = [7;3]; % initial conditions
% numerical solution using ode45
sol = ode45(funpr12,xint,y0); %// Change - already a handle
xx = [0:0.01:5]; % vector of x values
y = deval(sol,xx); % vector of y values
plot(xx,y(1,:),'r', 'LineWidth',3)
legend('y1(x)')
xlabel('x')
ylabel('y(x)')
Take note that the output when simulating the solution to the differential equation by deval will be a two column matrix. The first column is the solution to the system while the second column is the derivative of the solution. As such, you'll want to plot the first column, which is what the plot syntax is doing.
I get this plot now:
Hope everyone is doing well. Can anyone help me in solving 3 equations involving 2 unknown variables in Matlab?
I tried using symbolic toolbox but it gives me an empty solution. The Matlab code works for 2 equations only. For three equations it gives me an empty solution.
The equation are mentioned below:
cos^2(2\phi) cos(2\theta + sin(2\phi)cos(2\phi) sin(2\theta)=0
sin(2\phi)cos(2\phi)cos(2\theta)+sin^2(2\phi)sin(2\theta)=0
-sin(2\phi)cos(2\theta)+cos(2\phi)sin(2\theta)=0
I want to solve the above equations for theta and phi.
Can anyone help me in solving 3 eq for 2 variables in Matlab, I tried a lot using symbolic tool box but it gives me empty solution. The Matlab code works for 2 equation only. For three equation gives me empty solution.
The simplest explanation is that what Matlab is telling you is correct: there is no solution for the system of three equations.
One way to find out what's happening is to plot the solutions for the three equations individually. First take each equation and define the left side as a function in Matlab:
fun1 = #(phi, theta) cos(2*phi).^2 .* cos(2*theta) + sin(2*phi) .* cos(2*phi) .* sin(2*theta);
fun2 = #(phi, theta) sin(2*phi) .* cos(2*phi) .* cos(2*theta) + sin(2*phi).^2 .* sin(2*theta);
fun3 = #(phi, theta) -sin(2*phi) .* cos(2*theta) + cos(2*phi) .* sin(2*theta);
Now you can use ezplot(fun1) to plot the solutions to the implicit function fun1(x,y)=0. The solutions will be curves in the (theta, phi) plane. If you do this for fun2 and fun3 as well, the simultaneous solution will be represented by places where the curves from all three functions simultaneously intersect.
I don't know how to pile up three the three ezplot plots on the same axes, but we can use contour to accomplish the same thing:
x = linspace(-pi/2, pi/2, 180);
y = linspace(-pi/2, pi/2, 180);
[X, Y] = meshgrid(x, y);
contour(X, Y, fun1(X, Y), [0, 0], 'r', 'linewidth', 3);
hold all
contour(X, Y, fun2(X, Y), [0, 0], 'b');
contour(X, Y, fun3(X, Y), [0, 0], 'g');
hold off
legend('f_1(\theta,\phi)=0','f_2(\theta,\phi)=0','f_3(\theta,\phi)=0');
xlabel('\phi');
ylabel('\theta');
Which produces this graph:
We see that there are no points where the red, blue, and green curves (corresponding to solutions of fun1(x,y)=0, fun2(x,y)=0, and fun3(x,y)=0, respectively) simultaneously intersect.
Thus your system of three equations has no solution.