Using interp1 in an ODE function [MATLAB] - matlab

I have here an ODE to solve that has a test parameter a.
I would like a to be a function of temperature T (in this code it is solved as X(2) since it varies with time).
I would like to base it off experimental values of a and T.
Hence, I am thinking of doing it along the lines of interp1.
Here is my test dummy ODE.
function xprime = RabbitTemp(t,X)
% Model of Rabbit Population
% where,
% Xo = Initial Population of Rabbits
% X(1) = Population density of Rabbit
% X(2) = Temperature T (that varies with time)
% a = test parameter
% Interpolate here
Texptdata = [1 2 3 4];
aexptdata =[10 14 19 30];
a= interp1(Texptdata,aexptdata,X(2),'spline');
% ODE
dx = [0 0];
dx(1) = (X(1))*(1 - X(1)*a - 3*(X(2))));
dx(2) = sin(t);
xprime = [dx(1) dx(2)]';
end
I've tried running the code with my solver and it does compile fine.
However I am just wondering if the ODE solver is calling the correct interpolated a corresponding to the correct corresponding value of X(2). I cannot seem to figure out how to verify this.
Can anyone help me verify this or point me to how I can go about doing it? Much appreciated!

Related

How to miss out matrix elements in pcolor plot?

I'm solving a set of nonlinear simultaneous equations using Matlab's fsolve to find unknown parameters x1 and x2. The simultaneous equations have two independent parameters a and b, as defined in the root2d function:
function F = root2d(x,a,b)
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(a+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - b;
end
I use the following code to solve the simultaneous equations, and plot the results as a 2d figure using pcolor.
alist = linspace(0.6,1.2,10);
blist = linspace(0.4,0.8,5);
% results
x1list = zeros(length(blist),length(alist));
x2list = zeros(length(blist),length(alist));
% solver options
options = optimoptions('fsolve','Display','None');
for ii = 1:length(blist)
b = blist(ii);
for jj = 1:length(alist)
a = alist(jj);
x0 = [0 0]; % init guess
[xopt,yopt,exitflag] = fsolve(#(x0)root2d(x0,a,b),x0,options);
% optimised values
x1list(ii,jj) = xopt(1);
x2list(ii,jj) = xopt(2);
success(ii,jj) = exitflag; % did solver succeed?
end
end
% plotting
figure
s = pcolor(alist(success>0),blist(success>0),x1list(success>0));
xlabel('a')
ylabel('b')
title('my data x_1')
figure
s = pcolor(alist(success>0),blist(success>0),x2list(success>0));
xlabel('a')
ylabel('b')
title('my data x_2')
However I only want to plot the x1 and x2 where the solver has successfully converged to a solution. This is where the success matrix element (or exitflag) has a value greater than 0. Usually you just write x1list(success>0) when using the plot function and Matlab omits any solutions where (success<=0), but pcolor doesn't have that functionality.
Is there a way around this? For example, displaying all (success<=0) solutions as a black area.
Yes there is!
The easiest way is to make them NaN, so they are simply not drawn.
just do
x1list(~success)=NaN;
pcolor(alist,blist,x1list)

ode45 for Langevin equation

I have a question about the use of Matlab to compute solution of stochastic differentials equations. The equations are the 2.2a,b, page 3, in this paper (PDF).
My professor suggested using ode45 with a small time step, but the results do not match with those in the article. In particular the time series and the pdf. I also have a doubt about the definition of the white noise in the function.
Here the code for the integration function:
function dVdt = R_Lang( t,V )
global sigma lambda alpha
W1=sigma*randn(1,1);
W2=sigma*randn(1,1);
dVdt=[alpha*V(1)+lambda*V(1)^3+1/V(1)*0.5*sigma^2+W1;
sigma/V(1)*W2];
end
Main script:
clear variables
close all
global sigma lambda alpha
sigma=sqrt(2*0.0028);
alpha=3.81;
lambda=-5604;
tspan=[0,10];
options = odeset('RelTol',1E-6,'AbsTol',1E-6,'MaxStep',0.05);
A0=random('norm',0,0.5,[2,1]);
[t,L]=ode45(#(t,L) R_Lang(t,L),tspan,A0,options);
If you have any suggestions I'd be grateful.
Here the new code to confront my EM method and 'sde_euler'.
lambda = -5604;
sigma=sqrt(2*0.0028) ;
Rzero = 0.03; % problem parameters
phizero=-1;
dt=1e-5;
T = 0:dt:10;
N=length(T);
Xi1 = sigma*randn(1,N); % Gaussian Noise with variance=sigma^2
Xi2 = sigma*randn(1,N);
alpha=3.81;
Rem = zeros(1,N); % preallocate for efficiency
Rtemp = Rzero;
phiem = zeros(1,N); % preallocate for efficiency
phitemp = phizero;
for j = 1:N
Rtemp = Rtemp + dt*(alpha*Rtemp+lambda*Rtemp^3+sigma^2/(2*Rtemp)) + sigma*Xi1(j);
phitemp=phitemp+sigma/Rtemp*Xi2(j);
phiem(j)=phitemp;
Rem(j) = Rtemp;
end
f = #(t,V)[alpha*V(1)+lambda*V(1)^3+0.5*sigma^2/V(1)/2;
0]; % Drift function
g = #(t,V)[sigma;
sigma/V(1)]; % Diffusion function
A0 = [0.03;0]; % 2-by-1 initial condition
opts = sdeset('RandSeed',1,'SDEType','Ito'); % Set random seed, use Ito formulation
L = sde_euler(f,g,T,A0,opts);
plot(T,Rem,'r')
hold on
plot(T,L(:,1),'b')
Thanks again for the help !
ODEs and SDEs are very different and one should not use tools for ODEs, like ode45, to try to solve SDEs. Looking at the paper you linked to, they used a basic Euler-Maruyama scheme to integrate the system. This a very simple solver to implement yourself.
Before proceeding, you (and your professor!) should take some time to read up on SDEs and how to solve them numerically. I recommend this paper, which includes many Matlab examples:
Desmond J. Higham, 2001, An Algorithmic Introduction to Numerical Simulation of Stochastic Differential Equations, SIAM Rev. (Educ. Sect.), 43 525–46. http://dx.doi.org/10.1137/S0036144500378302
The URL to the Matlab files in the paper won't work; use this one. Note, that as this a 15-year old paper, some of the code related to random number generation is out of date (use rng(1) instead of randn('state',1) to seed the generator).
If you are familiar with ode45 you might look at my SDETools Matlab toolbox on GitHub. It was designed to be fast and has an interface that works very similarly to Matlab's ODE suite. Here is how you might code up your example using the Euler-Maruyma solver:
sigma = 1e-1*sqrt(2*0.0028);
lambda = -5604;
alpha = 3.81;
f = #(t,V)[alpha*V(1)+lambda*V(1)^3+0.5*sigma^2/V(1);
0]; % Drift function
g = #(t,V)[sigma;
sigma/V(1)]; % Diffusion function
dt = 1e-3; % Time step
t = 0:dt:10; % Time vector
A0 = [0.03;-2]; % 2-by-1 initial condition
opts = sdeset('RandSeed',1,'SDEType','Ito'); % Set random seed, use Ito formulation
L = sde_euler(f,g,t,A0,opts); % Integrate
figure;
subplot(211);
plot(t,L(:,2));
ylabel('\phi');
subplot(212);
plot(t,L(:,1));
ylabel('r');
xlabel('t');
I had to reduce the size of sigma or the noise was so large that it could cause the radius variable to go negative. I'm not sure if the paper discusses how they handle this singularity. You can try the 'NonNegative' option within sdeset to try to handle this or you may need to construct your own solver. I also couldn't find what integration time step the paper used. You should also consider contacting the authors of the paper directly.
UPDATE
Here's an Euler-Maruyama implementation that matches the sde_euler code above:
sigma = 1e-1*sqrt(2*0.0028);
lambda = -5604;
alpha = 3.81;
f = #(t,V)[alpha*V(1)+lambda*V(1)^3+0.5*sigma^2/V(1);
0]; % Drift function
g = #(t,V)[sigma;
sigma/V(1)]; % Diffusion function
dt = 1e-3; % Time step
t = 0:dt:10; % Time vector
A0 = [0.03;-2]; % 2-by-1 initial condition
% Create and initialize state vector (L here is transposed relative to sde_euler output)
lt = length(t);
n = length(A0);
L = zeros(n,lt);
L(:,1) = A0;
% Set seed and pre-calculate Wiener increments with order matching sde_euler
rng(1);
r = sqrt(dt)*randn(lt-1,n).';
% General Euler-Maruyama integration loop
for i = 1:lt-1
L(:,i+1) = L(:,i)+f(t(i),L(:,i))*dt+r(:,i).*g(t(i),L(:,i));
end
figure;
subplot(211);
plot(t,L(2,:));
ylabel('\phi');
subplot(212);
plot(t,L(1,:));
ylabel('r');
xlabel('t');

Extract the values for some parameters from stochastic differential equation solution

I am solving stochastic differential equation in matlab.
For example:
consider the stochastic differential equation
dx=k A(x,t)dt+ B(x,t)dW(t)
where k is constants, A and B are functions, and dW(t) is Wiener process.
I plot the solution for all t in [0,20]. We know that dW(t) is randomly generated. My question is: I want to know the value of A(x,t), B(x,t), dW(t) for a particular value of t and for particular sub-interval, say [3,6]. What command in Matlab I can use?
Here is the code I used based on a paper by D.Higham:
clear all
close all
t0 = 0; % start time of simulation
tend = 20; % end time
m=2^9; %number of steps in each Brownian path
deltat= tend/m; % time increment for each Brownian path
D=0.1; %diffsuion
R=4;
dt = R*deltat;
dW=sqrt( deltat)*randn(2,m);
theta0=pi*rand(1);
phi0=2*pi*rand(1);
P_initial=[ theta0; phi0];
L = m/ R;
pem=zeros(2,L);
EM_rescale=zeros(2,L);
ptemp=P_initial;
for j=1:L
Winc = sum(dW(:,[ R*(j-1)+1: R*j]),2);
theta=ptemp(1);% updating theta
phi=ptemp(2); % updating phi
%psi=ptemp(3); % updating psi
A=[ D.*cot(theta);...
0];% updating the drift
B=[sqrt(D) 0 ;...
0 sqrt(D)./sin(theta) ]; %% updating the diffusion function
ptemp=ptemp+ dt*A+B*Winc;
pem(1,j)=ptemp(1);%store theta
pem(2,j)=ptemp(2);%store phi
EM_rescale(1,j)=mod(pem(1,j),pi); % re-scale theta
EM_rescale(2,j)=mod(pem(2,j),2*pi); % re-scale phi
end
plot([0:dt:tend],[P_initial,EM_rescale],'--*')
Suppose I want to know all parameters (including random: Brownian) at each specific time point or for any time interval. How to do that?
I'm doing my best to understand your question here, but it's still a bit unclear to me.
Change the loop to:
for ii=1:L
Winc = sum(dW(:,[ R*(ii-1)+1: R*ii]),2);
theta=ptemp(1);% updating theta
phi=ptemp(2); % updating phi
A{ii}=[ D.*cot(theta);...
0];% updating the drift
B{ii}=[sqrt(D) 0 ;...
0 sqrt(D)./sin(theta) ]; %% updating the diffusion function
ptemp = ptemp + dt*A{ii}+B{ii}*Winc;
pem(:,ii) = ptemp;
EM_rescale(1,ii) = mod(pem(1,ii),pi); % re-scale theta
EM_rescale(2,ii) = mod(pem(2,ii),2*pi); % re-scale phi
end
Now, you can get the values of A and B this way:
t = 3;
t_num = round(m/tend*t);
A{t_num}
B{t_num}
ans =
0.0690031455719538
0
ans =
0.316227766016838 0
0 0.38420611784333

Finding solution to Cauchy prob. in Matlab

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:

Combining for-loops/while-loops and ODE45

I was wondering if it's possible to combine for-loops/while-loops and ODE45?, please look at the example below:
I have a function (ode) which I want to solve at different ic (initial conditions):
ser = #(x) x.^(-0.3) - x.^(1.8); % the function
tspan = 1:0.02:2;
x0 = 0.5;
% x0 = 0.8;
% x0 = 1.2;
% x0 = 1.8;
% x0 = 2;
% x0 = 2.5;
[~, x_t] = ode45(#(t,x) ser(x), tspan, x0);
plot(tspan,x_t,'r-')
And plot the solution curves at the end.
Is there a way to pass the other icwithout doing it manually, like a loop? or any other way to optimize this step? If I could receive some help with this, it will be very appreciated because I have to compute many ODEs (more complex ones) at 15 to 25 different ic.
Thanks in advance!
PS. If the code has to change (e.g. different names for the x0s or solutions x_t) it will be OK!
If you have a predefined no. of ic you can use this for loop:
for i=1:NoOfic
test_mat(i,:)=test_ode_45(x(i),tspan);
end
You should predefine your matrix (test_mat) in a sufficient dimension e.g.
test_mat = zeros(NoOfic, SizeOftspan)
Obviously I have defined the variables NoOficand SizeOftspanto the specific numbers
My whole test-code is:
tspan = 1:0.02:2;
x= [0.4 0.8 1.2 1.5 2.6];
sizeOftspan = size(tspan);
sizeOfFamily= size(x);
test_mat = zeros(sizeOfFamily(2),sizeOftspan(2));
test_tes= test_ode_45(x(1),tspan);
for i=1:sizeOfFamily(2)
test_mat(i,:)=test_ode_45(x(i),tspan);
end
plot(tspan, test_mat)
Where my test_ode_45 function is a simple x*sin(t) function