ODE45 with a vector input

조회 수: 8 (최근 30일)
Daniel Grebler
Daniel Grebler 2018년 6월 28일
댓글: Torsten 2018년 6월 29일
Hey, how can I solve an ode function with a changing input of my choosing: example:
I would like to solve the simple spring and defuse equation while inputting the external force at given time.
I would create a function for the differential equations:
if true
function dy = springdef(y,u)
k=1.5; %just values
m=3;
d=0.3;
% main differential equations
dy(1,1) = y(2);
dy(2,1) = -k/m*y(1)+u/m-d/m*y(2);
end
than I would calculate the result by using the ode45
if true
[t,y] = ode45(@(t,y)springdef(y,u),tspan,y0);
end
yet I don't find a way to choose u in advance (creating a vector for u for different values).
I can define a single input - u=0
or i can have a controller adjust the input - u= K*(y-[0,0]) but I don't find a way to preset the force u
such as u=[0,0,1,1,0,0] (of curse length(u) = length(tspan) )

채택된 답변

Torsten
Torsten 2018년 6월 28일
The example
"ODE with Time-Dependent Terms" under
https://de.mathworks.com/help/matlab/ref/ode45.html
should show you how to proceed.
Best wishes
Torsten.
  댓글 수: 2
Daniel Grebler
Daniel Grebler 2018년 6월 28일
Thank you Torsten, Allow me to ask another question:
How could I limit the max value of a solution (ode45 solution) without stopping the iterations
I can set a 'NoneNgeative' restrain I can think of maybe creating another variable such as
y(new)=y(restrained)-maxvalue
and have a NoneNegative on y(new) but how can I fuse it in the ode function since it has no derivative
my ode function is:
function dy = flywheeldef(t,y,u)
dy(1,1) = y(2);
dy(2,1) = (b*sin(y(1)))/a-(u/a);
dy(3,1) = y(4);
dy(4,1) = -b*sin(y(1))/a+u*(a+I2)/(I2*a);
--- how can I restrain y(4) to a max value? (and continue calculation)
Torsten
Torsten 2018년 6월 29일
The ODEs uniquely determine the solution - you can't constrain a variable to a max value. If you want to do this, you will have to adapt your u dependent on time.
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by