solving ode in a given grid

조회 수: 6 (최근 30일)
Koschey Bessmertny
Koschey Bessmertny 2022년 4월 29일
댓글: Koschey Bessmertny 2022년 5월 5일
I need to solve the system of ode
where t is the parameter. I know the analytical form of the matrix . However the parameter t is the function of x given in the discrete points , i.e. I know only the values . If I use the function in the form ode45(@rhs, [x1, x2, ... xn], initial condition), MATLAB should calculate the rhs of the equation, i.e. the matrix in the points . However, how to let MATLAB know that it should also use the propper values of the parameter t in certain grid points? I mean, I need to have , where .
For example, I solve the equation , where , with . The exact solution is . The main code is
clear all
global t
x = 0:.1:1;
t = x.^2;
[X,Y] = ode45(@test45rhs,x,1);
plot(Y)
I create a function
function dy = test45rhs(x,y)
global t
dy = - t.*y./x;
%dy = - x.*y;
It does not work.
However, if I modify the function
function dy = test45rhs(x,y)
global t
%dy = - t.*y./x;
dy = - x.*y;
Everything works.
  댓글 수: 2
Jan
Jan 2022년 4월 29일
What about calling the integration inside a loop over k?
Koschey Bessmertny
Koschey Bessmertny 2022년 5월 5일
I guess it is the only solution. Thanks

댓글을 달려면 로그인하십시오.

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2022년 4월 29일
If you have the analytical function for how your t depends on x you should be able to calculate t for any x inside your ODE-function. A modification of your first example along these lines ought to do it:
function dy = test45rhs(x,y)
t = x^2;
dy = - t.*y./x;
%dy = - x.*y;
If you only have t sampled at a set of points x_i then you might do something like this (provided that the underlying relation between t and x is smooth enough):
function dy = test45rhs(x,y,x_i,t_i)
t = interp1(x_i,t_i,x,'pchip');
dy = - t.*y./x;
%dy = - x.*y;
For this case you'll also have to modify the call to ode45 to something like this:
clear all
global t
x = 0:.1:1;
x_i = x; % for readability
t = x_i.^2; % Just using your illustrating example
[X,Y] = ode45(@(x,y) test45rhs(x,y,x_i,t_i),x,1);
plot(Y)
Now the "best-interpolated" value of t will be calculated for every call of test45rhs at "the current x-value".
HTH
  댓글 수: 2
Bjorn Gustavsson
Bjorn Gustavsson 2022년 5월 5일
Did this answer solve your problem?
Koschey Bessmertny
Koschey Bessmertny 2022년 5월 5일
Thanks for the answer. The interpolation is slow in matlab. I decided to use the Euler integration in a loop.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by