Plotting a function on a specified domain - matlab

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)).

Related

Plotting three functions in the same output

I've defined the function
function dzdt=odefun2222(t,z)
beta=1+exp(-t); delta=1+exp(-t); rho=1+exp(-t);
gama=exp(-t); theta=exp(-t);
f1=1/2+1/(t+1); f2=1/2+1/(t+1);
dzdt=zeros(6,1);
dzdt(1)=z(2)-f1*z(1);
dzdt(2)=(1/4+1/(t+1)-beta)*z(1)+gama*z(3)-f1*z(2)-f1*(z(1)^2);
dzdt(3)=z(4);
dzdt(4)=gama*z(1)-delta*z(3)+theta*z(5);
dzdt(5)=z(6)-f2*z(5);
dzdt(6)=(1/4+1/(t+1)-rho)*z(5)+theta*z(3)-f2*z(6)-f2*(z(5)^2);
end
and I've used the commands
tspan = [0 20];
z0 = [0.001 0.001 0.001 0.001 0.001 0.001];
[t,z] = ode45(#(t,z) odefun2222(t,z), tspan, z0);
plot(t,z(:,1),'b',t,z(:,3),'r',t,z(:,5),'g')
I've obtained the plotting only of z(3) and z(5), z(1) is not plotted at all. What can I do in order to get the plotting of all three functions z(1), z(3), z(5) in the same output ?

Plotting a function together with a range of different constant value parameters

Mathematica easily plots a function with constant value parameters over a range of different values for the constant(s).
With MATLAB, is this possible?
For instance: would MATLAB allow me to plot f(x)=x*e^a of different constant values of a?
Depends how you define easily:
>> alpha = [0.1 0.2 0.5 1];
>> x = 0:0.01:2;
>> plot(x,exp(x(:)*alpha))
>> grid on
>> legend(arrayfun(#(a)sprintf('alpha = %.2f',a),alpha,'uni',false))
fplot() can also be used to plot functions.
alpha = [0.1 0.5 1];
f = (#(x) exp(alpha.*x));
fplot(f, [0,2]); grid on; % Axis range = [0, 2]
legend('alpha=0.1', 'alpha=0.5', 'alpha=1.0');
And the output is:

Plotting a function defined by a system of ODEs with MATLAB

I am trying to solve with MATLAB the first order ODEs system,
$$\left\{
\begin{array}{l}
x_{1}^{\prime }=-\frac{1}{t+1}x_{1}+x_{2} \\
x_{2}^{\prime }=-(1+e^{-2t})x_{1}-\frac{1}{t+1}x_{2}+\frac{e^{-3t}}{t+1}x_{3}
\\
x_{3}^{\prime }=-\frac{1}{t+1}x_{3}+x_{4} \\
x_{4}^{\prime }=e^{-3t}\left( t+1\right) x_{1}-\left( 1+e^{-2t}\right) x_{3}-%
\frac{1}{t+1}x_{4}-\frac{1}{t+1}x_{3}^{2}%
\end{array}%
\right. $$
I've defined the function:
function dzdt=odefun(t,z)
dzdt=zeros(4,1);
dzdt(1)=-(1/(t+1))*z(1)+z(2);
dzdt(2)=-(1+exp(-2*t))*z(1)-(1/(t+1))*z(2)+(exp(-3*t))/(t+1)*z(3);
dzdt(3)=z(4)-(1/(t+1))*z(3);
dzdt(4)=(exp(-3*t))*(t+1)*z(1)-(1+exp(-2*t))*z(3)-(1/(t+1))*z(4)-(1/(t+1))*z(3)^2;
end
The time interval is [0,100] and the initial conditions are z0 = [0.01 0.01 0.01 0.01].
With the ode45 solver, I've used the commands:
>> tspan = [0 100];
>> z0 = [0.01 0.01 0.01 0.01];
>> [t,z] = ode45(#(t,z) odefun(t,z), tspan, z0);
>> plot(t,z(:,1),'r')
and I've obtained easily the graph of z(1)=x_1.
But I want to plot the function f(t)=(t+1)*x_1(t), t\in [0,100], where x_1=z(1) is the first unknown of the system. How could I do this ?
Just use per element multiplication .*
plot((t+1).*z(:,1))

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