some quetions about ode45
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jack
el 13 de Abr. de 2020
Comentada: Ameer Hamza
el 24 de Abr. de 2020
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 comentarios
Ameer Hamza
el 14 de Abr. de 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.
Respuesta aceptada
Steven Lord
el 14 de Abr. de 2020
1.in following code,what‘s the effect of symbol '[ ]'? why must use it?
[t,uvtsol]=ode45('wave1D',t1,u0,[],N,k,a);
That's an old, now undocumented syntax for ode45. The [] indicates you have no options that you want to pass into ode45 but you want to pass additional inputs into the ODE function wave1D. That syntax does work for ode45 for backwards compatibility, but newer "function functions" (like the DDE solver ddesd) likely will not accept that syntax. ddesd does not. The recommended approach that I usually use for passing additional inputs into an ODE function is:
[t,uvtsol]=ode45(@(t, y) wave1D(t,y,N,k,a),t1,u0);
2.what's the effect of symbol 'dummy'?why must use it?
function dudt=wave1D(t,u,dummy,N,k,a)
Because you're using the old, undocumented syntax. If you use the techniques given on that documentation page, you don't need that "dummy" argument.
2 comentarios
Ameer Hamza
el 24 de Abr. de 2020
Jack's comment from "flag" move here:
I have test the code which @Steven Lord give in MATLAB2020a,all is Ok
@Jack, flag is used to indicate a negative response. It is appropriate that you add a comment if the solution works for you.
Más respuestas (1)
Ameer Hamza
el 14 de Abr. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!