Info

この質問は閉じられています。 編集または回答するには再度開いてください。

can anyone help me with "ode45" ?

1 回表示 (過去 30 日間)
ADNAN KIRAL
ADNAN KIRAL 2018 年 11 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi
i do have a function like;
function [xdot,NOD]=random_func(t,x)
.
.
.
end
and a "ode45" like
[t,x]=ode45(@random_func,tspan,x0,cn3);
I would like to take "NOD" out given in my function;
How can I get it in a ".mat" ? is that possible? Or I need to use the Runge Kutta method manually?
Please a bit urgent, thanks in advance. If you want a clear understanding, I can upload a draft code.
  2 件のコメント
James Tursa
James Tursa 2018 年 11 月 26 日
編集済み: James Tursa 2018 年 11 月 26 日
Note that you cannot do anything random inside a derivative function that affects the derivative calculation. This violates an assumption that ode45 makes, will severly confuse it, and will cause it to either fail outright or give you a wrong answer.
ADNAN KIRAL
ADNAN KIRAL 2018 年 11 月 27 日
i see, will consider that. thanks for your reply.

回答 (1 件)

Star Strider
Star Strider 2018 年 11 月 25 日
Probably the easiest way would be to integrate your function first, the using the solved values for ‘t’ and ‘x’, run it again, recover the ‘NOD’ output, and save it.
Example —
function [xdot,NOD]=random_func(t,x)
xdot(1,:) = t-x(1); % Create Equation
xdot(2,:) = t+x(2); % Create Equation
NOD = rand; % Create ‘NOD’
end
tspan = [0 1];
x0 = [1;1];
[T,X] = ode45(@random_func, tspan, x0);
figure
plot(T, X)
grid
NODv = zeros(numel(T),1); % Preallocate
for k1 = 1:numel(T)
[~,NOD(k1,:)] = random_func(T(k1),X(k1,:)); % Calculate ‘NOD’, Save As Vector
end
fprintf(1, '%.4f,%.4f\n', [T NOD]') % Save Data
I simply printed out the ‘NOD’ vector here (alternatively saving it as a ‘.csv’ file with the same statement although with an appropriate file identification number created by fopen), since I did not want to create the file and then have to delete it. Use the appropriate save syntax to create your ‘.mat’ file.
This illustrates the idea. You will likely need to experiment with it and tweak it to make it work with your functions.
  2 件のコメント
ADNAN KIRAL
ADNAN KIRAL 2018 年 11 月 26 日
thanks for reply.
Star Strider
Star Strider 2018 年 11 月 26 日
My pleasure.

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by