I am having trouble multiplying my main ode function with an external function NS which is supposed to be multiplied on the RHS. Thanks for the great help.

1 回表示 (過去 30 日間)
tspan = [0 20];
y0 = [0 0.01];
[z,y] = ode45(@odefcn, tspan, y0);
plot(z,y(:,1),'-o',z,y(:,2),'-.')
function dydt = odefcn(z,y)
dydt1 = y(2);
dydt2 = NS*z.*y(1);
dydt=[dydt1;dydt2];
end
function M = NS(z)
z = [2 3 5 7 10 15 20 ];
r =[3.5 3.7 4 6 7.2 8 9];
n=length(z);
% Calculation of differentiation from the above datas
for i=1:n-1
M=zeros();
M(i)=(r(i+1)-r(i))./(z(i+1)-z(i));
end
end
  3 件のコメント
Dereje
Dereje 2017 年 11 月 30 日
yess you are right Torsten, now I corrected to z(I was supposed to mean z not t)
Dereje
Dereje 2017 年 11 月 30 日
I wanted to solve a function with the name odefcn. But my problem is the RHS is supposed to be multiplied with external function which is differentiation of data points.

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

採用された回答

Torsten
Torsten 2017 年 11 月 30 日
zr = [2 3 5 7 10 15 20];
r = [3.5 3.7 4 6 7.2 8 9];
y0 = [0 ; 0.01];
zsol = [];
y1sol = [];
y2sol = [];
for i=1:numel(r)-1
zspan = [zr(i) zr(i+1)];
NS = (r(i+1)-r(i))/(zr(i+1)-zr(i));
[z,y] = ode45(@(z,y)odefcn(z,y,NS), zspan, y0);
y0 = [y(end,1) ; y(end,2)];
zsol = [zsol;z];
y1sol = [y1sol;y(:,1)];
y2sol = [y2sol;y(:,2)];
end
plot(zsol,y1sol,zsol,y2sol)
function dydt = odefcn(z,y,NS)
dydt1 = y(2);
dydt2 = NS*z*y(1);
dydt=[dydt1;dydt2];
end
Best wishes
Torsten.
  10 件のコメント
Torsten
Torsten 2017 年 12 月 1 日
Using ode15s instead of ode45 might reduce the computation time.
Best wishes
Torsten.
Dereje
Dereje 2017 年 12 月 1 日
ya a little better! Thanks!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by