How do I extract an intermediate variable calculated and used inside my ode45 function?
조회 수: 37 (최근 30일)
이전 댓글 표시
Hello,
I'm using ode45 to produce solutions to ODE's and It works perfectly with me. However, I have a variable that depends on the current differentiated variable ,say x as in my example below. This variable is computed inside my function which then used in my differential equations. This is my ode call:
[T,X] = ode45(@(t,x) myode(t,x,P,BK,R), tspan, x0);
and here is my function:(the variable I need to extract at each instant, i.e. it should have same length as X and T, is u)
function dx=myode(t,x,P,BK,R)
x1=x(1); x2=x(2);
N=size(BK,1);
f1=x2;
f2=sin(x1)
f=[f1;f2]; %system vector field
gx=[0;cos(x1)];
phi=(ones(length(P),1)*x.').^BK(1:length(P),:);
phi=prod(phi,2);
u=-inv(R)*gx'*P'*phi;
if u>=1
u=1;
end
dx=f+gx*u;
end
Is it possible to get u? what is the best way to do that? I know similar questions were and are being asked a lot but I searched for the solution and couldn't find a workable one to my problem!
For a simpler example (because the variables I have as my inputs above are very large matrices) and we can work on this since the main goal is the same:
x0=[1;1]; tspan=[0 1]; R=1;
[T,X] = ode45(@(t,x) myode(t,x,R), tspan, x0);
function dx=myode(t,x,R)
x1=x(1); x2=x(2);
f1=x2;
f2=sin(x1);
f=[f1;f2]; %system vector field
gx=[0;1];
u=-inv(R)*gx'*x;
if u>=1
u=1;
end
dx=f+gx*u;
end
I want to get the computed values of u at each time instant with t with x. In other words, I want to get an array with the computed values of u.
Thanks in advance. Your help is appreciated.
댓글 수: 2
madhan ravi
2018년 11월 11일
upload your question(equation) as latex form and provide all the necessary details
Stephen23
2018년 11월 11일
편집: Stephen23
2018년 11월 11일
Note that this line is probably not very efficient or accurate:
u=-inv(R)*gx'*P'*phi;
The inv and * should probably be replaced by mldivide. If you have large arrays then this will be significantly more efficient, and well as being more accurate numerically.
채택된 답변
Stephen23
2018년 11월 11일
편집: Stephen23
2018년 11월 11일
"I want to get the computed values of u at each time instant with t with x"
The simplest solution is to add a second output argument u:
function [dx,u] = myode(t,x,P,BK,R)
Then call your function normally to solve:
[T,X] = ode45(@(t,x) myode(t,x,P,BK,R), tspan, x0);
and then, now that you have the final T and X values, calculate the U values:
[~,U] = cellfun(@(t,x) myode(t,x.',R), num2cell(T), num2cell(X,2),'uni',0);
You could try it without the 'uni' option too, it might work. Otherwise simply convert the cell array to a matrix:
U = cell2mat(U);
댓글 수: 3
Stephen23
2018년 11월 11일
편집: Stephen23
2018년 11월 11일
@Has MBK: I forgot about the dimensions of X. See my edited answer using num2cell.
I tried this on your simple example function:
>> [T,X] = ode45(@(t,x) myode(t,x,R), tspan, x0)
T =
0.00000
0.06813
0.16813
0.26813
0.36813
0.46813
0.56813
0.66813
0.76813
0.86813
0.96813
1.00000
X =
1.00000 1.00000
1.06780 0.99074
1.16639 0.98199
1.26435 0.97775
1.36205 0.97671
1.45975 0.97769
1.55762 0.97960
1.65567 0.98144
1.75387 0.98230
1.85207 0.98138
1.95006 0.97794
1.98120 0.97622
>> [~,U] = cellfun(@(t,x) myode(t,x.',R), num2cell(T), num2cell(X,2),'uni',0);
>> cell2mat(U)
ans =
-1.00000
-0.99074
-0.98199
-0.97775
-0.97671
-0.97769
-0.97960
-0.98144
-0.98230
-0.98138
-0.97794
-0.97622
The only change I made to your ode function was to add u to the output arguments:
function [dx,u] = myode(t,x,R)
추가 답변 (3개)
Walter Roberson
2018년 11월 11일
If the variable is to be the same length as t then you need to recalculate it afterwards. The ode* routines do not generally calculate at the returned t: they calculate at multiple times and use them to estimate the values that they return. In general the ode* routines may go forwards or backwards in time and may calculate with multiple boundary conditions at the same time point in order to be sure that they are within the error constraints.
Raghavendra Ragipani
2020년 2월 6일
Hi,
I had similar problem and I created functions to easily achieve this task. I decided to share it with the MATLAB community. Check it out at the following link.
Call save_var_in_ODE() function inside the ODE program and call get_var_in_ODE() in the main script file where you'll further manipulate it or plot it.
Example usage is given along with the package.
Cheers,
Raghav
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!