Function with form, that changes in time
古いコメントを表示
Hello,
I want to solve a differential equation with ode45, but one of my parameters is a function of time. It changes its form depending on the parameter t; for example, for t>0 and t<1 it's a linear function, but for t>=1 it's constant. How can I programme such parameter in MatLab?
採用された回答
その他の回答 (2 件)
Michal Mirowski
2018 年 1 月 29 日
編集済み: Michal Mirowski
2018 年 1 月 29 日
2 件のコメント
Walter Roberson
2018 年 1 月 29 日
You need to break that up into sections each place the function changes, if you want to use ode45.
Michal Mirowski
2018 年 1 月 29 日
Michal Mirowski
2018 年 1 月 29 日
編集済み: Michal Mirowski
2018 年 1 月 29 日
5 件のコメント
Walter Roberson
2018 年 1 月 29 日
function dy = rownania(t,y,Rt,Lt,Cc,Cm,Jm,Ut,Mo)
dy =[(Ut-Rt*y(1)-Cc*y(2))/Lt;
(Cm*y(1)-Mo(t)/Jm];
end
together with
Mo = @(t) 5*t + 5;
[T, Y] = ode45(@(t,y)rownania(t,y,Rt,Lt,Cc,Cm,Jm,Ut,Mo),[0 5],[0 0]);
plot(T,Y(:,2),'b-.');
yout = Y(end,:);
Mo = @(t) 30*ones(size(t));
[T, Y] = ode45(@(t,y)rownania(t,y,Rt,Lt,Cc,Cm,Jm,Ut,Mo),[5 10], yout);
plot(T,Y(:,2),'b-.');
yout = Y(end,:);
Mo = @(t) -3*t + 60;
[T, Y] = ode45(@(t,y)rownania(t,y,Rt,Lt,Cc,Cm,Jm,Ut,Mo),[10 20], yout);
plot(T,Y(:,2),'b-.');
yout = Y(end,:);
and so on.
Please note that for your first call
% CZĘŚĆ PIERWSZA - ROZWIĄZANIE RR NIEZALEŻNEGO OD Mo=f(t)
hold on
[T,Y]=ode45(@rownania,[0 20],[0 0]);
plot(T,Y(:,1),'g-');
that Mo has not been assigned a value yet and so because it is a global would be [] which would give you [] for the second dy result which would have failed. I have not shown how to replace that case because I do not know what is desired there. Perhaps
Mo = @(t) zeros(size(t));
Michal Mirowski
2018 年 1 月 30 日
Michal Mirowski
2018 年 1 月 30 日
Walter Roberson
2018 年 1 月 30 日
Do not loop on t. Those calls I showed are instead of looping.
I missed a ) when I was typing.
function dy = rownania(t,y,Rt,Lt,Cc,Cm,Jm,Ut,Mo)
dy =[(Ut-Rt*y(1)-Cc*y(2))/Lt;
(Cm*y(1)-Mo(t))/Jm];
end
Michal Mirowski
2018 年 1 月 30 日
編集済み: Michal Mirowski
2018 年 1 月 30 日
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!