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)).
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))
I am trying to generates the variable Fourier series for a square wave signal and a uniformly distributed noise signal.
This is my code and I have this error
Undefined function 'symsum' for input arguments of type 'double'.
fs = 1000; %Sampling frequency Hz [1 1000]
l = 1 ; %Signal length sec [1 10]
% Fourier Series Parameter
h = 1; %Amplitude [0 10]; default 1
k =1 ; %Fourier Series lenght(k);[1 21]
% Noise Signal specific parameters
h1 = 1; %Amplitude (h); [0 10]; default: 1
%calculate signal
%x = 0:1/fs:L-1/fs;
x = 0:1/fs:l-1;
n = [1; Inf];
f(x) = 4*h/pi * symsum( (1./k) * sin((2*pi*k*x)),k,2*n-1,n); %Fourier series for a square wave signal
%uniformly distributed noise signaL
%Noise signal specific parameters
%Amplitude (h); [0 10]; default: 1
h2 = 1 ; % Noise Amplitude
sig_Noise = h2*(f(x) + sigma*randn(1,length(x))); % signal with phase & amplitude noise
plot(x,f(x))
my summation is here Summation
how can I fix it?
This should do the trick:
fs = 1000; %Sampling Frequency Hz [1 1000]
l = 10; % Signal Length sec [1 10]
h = 1; % Amplitude [0 10] - Default 1
h2 = 1; % Noise Amplitude
sigma = 1;
x = 0:(1/fs):(l-1);
syms k;
syms f(x);
f(x) = 4*h/pi() * symsum((1/k)*sin(2*pi()*k*x),k,1,Inf); % Fourier series for a square wave signal
fplot(x,f(x));
Actually, if you want to work with symbolic maths... you have to work with symbolic variables that can be declared using the syms keyword. For plotting, the fplot function must be used instead of plot.
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 can I solve a 2nd order differential equation with boundary condition like z(inf)?
2(x+0.1)·z'' + 2.355·z' - 0.71·z = 0
z(0) = 1
z(inf) = 0
z'(0) = -4.805
I can't understand where the boundary value z(inf) is to be used in ode45() function.
I used in the following condition [z(0) z'(0) z(inf)], but this does not give accurate output.
function [T, Y]=test()
% some random x function
x = #(t) t;
t=[0 :.01 :7];
% integrate numerically
[T, Y] = ode45(#linearized, t, [1 -4.805 0]);
% plot the result
plot(T, Y(:,1))
% linearized ode
function dy = linearized(t,y)
dy = zeros(3,1);
dy(1) = y(2);
dy(2) = y(3);
dy(3) = (-2.355*y(2)+0.71*y(1))/((2*x(t))+0.2);
end
end
please help me to solve this differential equation.
You seem to have a fairly advanced problem on your hands, but very limited knowledge of MATLAB and/or ODE theory. I'm happy to explain more if you want, but that should be in chat (I'll invite you) or via personal e-mail (my last name AT the most popular mail service from Google DOT com)
Now that you've clarified a few things and explained the whole problem, things are a bit more clear and I was able to come up with a reasonable solution. I think the following is at least in the general direction of what you'd need to do:
function [tSpan, Y2, Y3] = test
%%# Parameters
%# Time parameters
tMax = 1e3;
tSpan = 0 : 0.01 : 7;
%# Initial values
y02 = [1 -4.805]; %# second-order ODE
y03 = [0 0 4.8403]; %# third-order ODE
%# Optimization options
opts = optimset(...
'display', 'off',...
'TolFun' , 1e-5,...
'TolX' , 1e-5);
%%# Main procedure
%# Find X so that z2(t,X) -> 0 for t -> inf
sol2 = fminsearch(#obj2, 0.9879680932400429, opts);
%# Plug this solution into the original
%# NOTE: we need dense output, which is done via deval()
Z = ode45(#(t,y) linearized2(t,y,sol2), [0 tMax], y02);
%# plot the result
Y2 = deval(Z,tSpan,1);
plot(tSpan, Y2, 'b');
%# Find X so that z3(t,X) -> 1 for t -> inf
sol3 = fminsearch(#obj3, 1.215435887288112, opts);
%# Plug this solution into the original
[~, Y3] = ode45(#(t,y) linearized3(t,y,sol3), tSpan, y03);
%# plot the result
hold on, plot(tSpan, Y3(:,1), 'r');
%# Finish plots
legend('Second order ODE', 'Third order ODE')
xlabel('T [s]')
ylabel('Function value [-]');
%%# Helper functions
%# Function to optimize X for the second-order ODE
function val = obj2(X)
[~, y] = ode45(#(t,y) linearized2(t,y,X), [0 tMax], y02);
val = abs(y(end,1));
end
%# linearized second-order ODE with parameter X
function dy = linearized2(t,y,X)
dy = [
y(2)
(-2.355*y(2) + 0.71*y(1))/2/(X*t + 0.1)
];
end
%# Function to optimize X for the third-order ODE
function val = obj3(X3)
[~, y] = ode45(#(t,y) linearized3(t,y,X3), [0 tMax], y03);
val = abs(y(end,2) - 1);
end
%# linearized third-order ODE with parameters X and Z
function dy = linearized3(t,y,X)
zt = deval(Z, t, 1);
dy = [
y(2)
y(3)
(-1 -0.1*zt + y(2) -2.5*y(3))/2/(X*t + 0.1)
];
end
end
As in my comment above, I think you're confusing a couple of things. I suspect this is what is requested:
function [T,Y] = test
tMax = 1e3;
function val = obj(X)
[~, y] = ode45(#(t,y) linearized(t,y,X), [0 tMax], [1 -4.805]);
val = abs(y(end,1));
end
% linearized ode with parameter X
function dy = linearized(t,y,X)
dy = [
y(2)
(-2.355*y(2) + 0.71*y(1))/2/(X*t + 0.1)
];
end
% Find X so that z(t,X) -> 0 for t -> inf
sol = fminsearch(#obj, 0.9879);
% Plug this ssolution into the original
[T, Y] = ode45(#(t,y) linearized(t,y,sol), [0 tMax], [1 -4.805]);
% plot the result
plot(T, Y(:,1));
end
but I can't get it to converge anymore for tMax beyond 1000 seconds. You may be running into the limits of ode45 capabilities w.r.t. accuracy (switch to ode113), or your initial value is not accurate enough, etc.