some quetions about ode45
Show older comments
I try to use ode45 to deal some PDES by Method of line(MOL,https://en.wikipedia.org/wiki/Method_of_lines) and Fourier spectral Method .
I have some quetions about ode45,
1.in following code,what‘s the effect of symbol '[ ]'? why must use it?
[t,uvtsol]=ode45('wave1D',t1,u0,[],N,k,a);
2.what's the effect of symbol 'dummy'?why must use it?
function dudt=wave1D(t,u,dummy,N,k,a)
I have look for some material about these some quetions(https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html),however,
this can't answers my quetions.
Following is all code,
%main
clc;close all;
%some parameters
lowera = -40;
upperb= 40;
N = 256;
L=upperb-lowera;
dx = (upperb-lowera)/N;
x =lowera + dx*(0:N-1);
% fourier derivatives
Nx = size(x,2);
k = 2*pi/(upperb-lowera)*[0:Nx/2-1 0 -Nx/2+1:-1]';
u01=2*sech(x);
u01=fft(u01);
u02=zeros(1,N);
u0=[u01 u02];
a=1;t1=0:0.5:20;
[t,uvtsol]=ode45('wave1D',t1,u0,[],N,k,a);
usol=ifft(uvtsol(:,1:N),[],2);
function dudt=wave1D(t,u,dummy,N,k,a)
dudt1=u(N+1:2*N);
dudt2=-a^2*(k).^2.*u(1:N);
dudt=[dudt1;dudt2];
end
3 Comments
Ameer Hamza
on 13 Apr 2020
[t,uvtsol]=ode45('wave1D',t1,u0,[],N,k,a);
This is not the signature of MATLAB's ode45 function. Are you using ode45 from some other source?
Ameer Hamza
on 14 Apr 2020
I got confused because the documentation does not mention the signature of ode45 used in your code. It is using an undocumented behavior of ode45.
Accepted Answer
More Answers (1)
Ameer Hamza
on 14 Apr 2020
I got confused initially because the signature of the ode45 call used in your code does not appear in the documentation. This seems to be using some undocumented behavior of ode45. Current documentation describes the following way to call ode45 for your ODE.
%main
clc;close all;
%some parameters
lowera = -40;
upperb= 40;
N = 256;
L=upperb-lowera;
dx = (upperb-lowera)/N;
x =lowera + dx*(0:N-1);
% fourier derivatives
Nx = size(x,2);
k = 2*pi/(upperb-lowera)*[0:Nx/2-1 0 -Nx/2+1:-1]';
u01=2*sech(x);
u01=fft(u01);
u02=zeros(1,N);
u0=[u01 u02];
a=1;t1=0:0.5:20;
[t,uvtsol]=ode45(@(t,u) wave1D(t,u,N,k,a),t1,u0); % call as function handle
usol=ifft(uvtsol(:,1:N),[],2);
function dudt=wave1D(t,u,N,k,a) %
dudt1=u(N+1:2*N);
dudt2=-a^2*(k).^2.*u(1:N);
dudt=[dudt1;dudt2];
end
The usual way ode45 work is it takes an ODE function with two inputs, therefore in my case, I defined it as function handle like this
@(t,u) wave1D(t,u,N,k,a)
This is a function with two inputs. Since wave1D takes require input, we pass them as constants in an anonymous function.
Now coming to your code. Since it is undocumented so I can just speculate. In this line
[t,uvtsol]=ode45('wave1D',t1,u0,[],N,k,a);
The fourth input to ode45 is an odeset object. Since this is optional, this code just passes it an empty array [ ]. The remaining 3 inputs are simply passed on to wave1D. In your function definition, it uses dummy as the third input parameter
function dudt=wave1D(t,u,dummy,N,k,a)
I guess it was a requirement for this undocumented behavior.
I recommend using the new function call signatures to avoid confusion.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!