How to differential spline graph in the matlab - matlab

I'm the begginer of the matlab. So i can't find how to differential the spline graph. So could you explain me how to differential spline graph?
I wanna differential the graph below
x = -4:4;
y = [0 .15 1.12 2.36 2.36 1.46 .49 .06 0];
cs = spline(x,[0 y 0]);
xx = linspace(-4,4,101);
plot(ppval(cs,xx));

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 Harmonic oscillator numerically?

I am trying to solve, using MATLAB, the time dependent Harmonic oscillator equation numerically. But I have no idea how to even get started as I have never learned this method in university:
X'' + w(t)^2 X = 0
with boundary conditions X_0 = 1, X_0' = 0 and Y_0 = 0, Y'_0 = 1
ode45 is a good start point.
Basic examples :
clear;close all;clc
tspan=[0 100]; % time
x_init=[5;0]; % known initial conditions
[t,y] = ode45(#vdp1,tspan,x_init);
figure(1)
plot(t,y(:,1),'r',t,y(:,2),'b')
grid on
title('Solution with ODE45');
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
f=.5
[t,y] = ode45(#(t,y) vdp2(t,y,f),tspan,x_init);
figure(2)
plot(t,y(:,1),'r',t,y(:,2),'b')
grid on
title(['Solution with ODE45 and f = ' num2str(f)]);
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
support functions:
function dydt = vdp1(t,y)
%
f=.1;
dydt = [y(2); sin(2*pi*f*t)*y(1)];
function dydt = vdp2(t,y,f)
%
dydt = [y(2); sin(2*pi*f*t)*y(1)];
This: https://uk.mathworks.com/matlabcentral/fileexchange/69951-runge-kutta-fixed-step-solvers?s_tid=srchtitle_harmonic%2520oscillator_81 among other examples has example1 solving damped and driven harmonic oscillators
And another harmonic oscillator solution with ODE solver:
https://uk.mathworks.com/matlabcentral/fileexchange/83233-matlab_program_solving_odes_harmonic_oscillators?s_tid=srchtitle_harmonic%20oscillator_9
You can also solve with symbolic expressions, this is Loren's post comparing symbolic and numerical solving
https://blogs.mathworks.com/loren/2010/04/08/odes-from-symbolic-to-numeric-code/?s_tid=srchtitle_harmonic%2520oscillator_105
I find the following concise introduction to ODE solving by Cleve Moler more useful than some full year university modules.
https://blogs.mathworks.com/cleve/2016/11/14/my-favorite-ode/?s_tid=srchtitle_harmonic%2520oscillator_131
When solving oscillators in polar coordinates you may need to have a look at
https://blogs.mathworks.com/cleve/2017/11/06/three-term-recurrence-relations-and-bessel-functions/?s_tid=srchtitle_harmonic%2520oscillator_140
In point 6 there's a wonderful introduction to Bessel functions and their zeros.

Plotting a function on a specified domain

I am trying to plot the solution of a system of ODEs.
The code is:
tspan = [0 10];
z0 = [0.01 0.01 0.01 0.01];
[t,z] = ode45(#(t,z) odefun3(t,z), tspan, z0);
plot(z(:,3))
Why the output is plotted on the interval [0,60] and not on [0,10], as in the code ?
I fixed it, by adding the first variable under the plot command: plot(t,z(:,3)).

plot of 3 competing species with diffusion

I want to make a code for a model of competing three species with diffusion to get plots ( 2d plot u, v, w against time ) My model is
u'=d1u_xx+(r1-a1u-b1v-c1w)u
v'=d2v_xx+(r2-a2u-b2v-c2w)v
w'=d3w_xx+(r3-a3u-b3v-c3w)w
where d1=d2=d3=1;
r1=1.5; r2=2.65; r3=3.45;
a1=0.1; b1=0.3; c1=0.01; b2=0.2;
a2=0.3; c2=0.2; c3=0.2; a3=0.01; b3=0.1.
I did the code for this model without diffusion as follow
% Define diff. equations, time span and initial conditions, e.g.
tspan = [0 20];
y0 = [0.2 0.2 0.2];
r1=1.5; r2=2.65; r3=3.45;
a1=0.1; a2=0.3; a3=0.01;
b1=0.3; b2=0.2; b3=0.1;
c1=0.01; c2=0.2; c3=0.2;
dy = #(t,y) [
(r1-a1*y(1)-b1*y(2)-c1*y(3))*y(1);
(r2-a2*y(1)-b2*y(2)-c2*y(3))*y(2);
(r3-a3*y(1)-b3*y(2)-c3*y(3))*y(3)];
% Solve differential equations
[t,y] = ode45(dy, tspan, y0);
% Plot all species against time
figure(1)
plot(t,y)
Resulting in following figure:
how to do it with diffusion like in this paper figure 10u, v, w

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