How to load intermediate variable into workspace while using ode45 ?

2 ビュー (過去 30 日間)
Pritesh Patel
Pritesh Patel 2022 年 6 月 17 日
編集済み: Torsten 2022 年 6 月 24 日
function dx=system (t,x)
dx=-x(1);
a=1;
end
%%
function main
tspan=1:100;
x0=0;
[t,x]=ode45(@system, tspan,x0)
end
how to load variable "a" into the workspace ?

回答 (2 件)

Stephen23
Stephen23 2022 年 6 月 17 日
編集済み: Stephen23 2022 年 6 月 17 日
Here is the neat, easy, robust approach which returns exactly the a values at the exact t and x output values:
tspan = 1:100;
x0 = 0;
[t,x] = ode45(@system, tspan,x0);
[~,a] = arrayfun(@system,t,x)
a = 100×1
1 1 1 1 1 1 1 1 1 1
function [dx,a] = system(t,x)
dx = -x(1);
a = 1;
end
Note that trying to obtain the value generated during the ODE-solver routine is much more complex (although this is often what beginners think of, it is practically unsolvable because you have no easy way to distinguish between points which are kept vs. discarded, e.g. when the solver goes backwards a few steps. Not every call of the objective function corresponds to output values, and you have no way to know which... consider the implications of that).
  2 件のコメント
Pritesh Patel
Pritesh Patel 2022 年 6 月 23 日
編集済み: Torsten 2022 年 6 月 23 日
function [dx,ss]=system(t,x)
dx=-x;
ts=40;
ssActive = [1,2,3,4,1,3,4,2,4,3,3,4,3,3,1,2,3,4,1,3,4,2];
k=floor(t/ts);
ss = ssActive(k+1);
end
tspan = 1:200;
x0 = zeros(38,1);
[t,x] = ode45(@system, tspan,x0);
[~,ss] = arrayfun(@system,t,x)
I want to plot ss. dimension of the state vector is 38*1
Code suggested by you is giving some dimensional error.
Stephen23
Stephen23 2022 年 6 月 23 日
Ah, so in your actual problem x0 is not scalar.
tspan = 1:200;
x0 = zeros(38,1);
[t,x] = ode45(@system, tspan,x0);
[~,ss] = cellfun(@system,num2cell(t),num2cell(x,2),'uni',0);
ss = [ss{:}];
plot(t,ss)
function [dx,ss]=system(t,x)
dx=-x;
ts=40;
ssActive = [1,2,3,4,1,3,4,2,4,3,3,4,3,3,1,2,3,4,1,3,4,2];
k=floor(t/ts);
ss = ssActive(k+1);
end

サインインしてコメントする。


Torsten
Torsten 2022 年 6 月 23 日
tspan = 1:200;
x0 = zeros(38,1);
[t,x] = ode45(@system, tspan,x0);
ts=40;
ssActive = [1,2,3,4,1,3,4,2,4,3,3,4,3,3,1,2,3,4,1,3,4,2];
k=floor(t/ts);
ss = ssActive(k+1);
plot(t,ss)
function [dx]=system(t,x)
dx=-x;
end
  4 件のコメント
Pritesh Patel
Pritesh Patel 2022 年 6 月 24 日
編集済み: Pritesh Patel 2022 年 6 月 24 日
ts=40;
ssActive = [1,2,3,4,1,3,4,2,4,3,3,4,3,3,1,2,3,4,1,3,4,2];
k=floor(t/ts);
ss = ssActive(k+1);
If I am using above equation inside the "system" function then after running the ode45 I am not able to store values in the workspace and hence not able to plot it.
Torsten
Torsten 2022 年 6 月 24 日
編集済み: Torsten 2022 年 6 月 24 日
Do you need these 4 lines of code for the solution of your differential equation or only for postprocessing and plotting ?
If you need these 4 lines of code for the solution of your differential equation, please show how. In your code above, they are not used to calculate "dx".
If you need them for plotting, do it after ode45 has finished as I showed in the code I posted.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by