using gradient with ode45
显示 更早的评论
i have this code, where gradient(u,t) should be the derivative of u:
function [xdot]=pid_practica9_ejercicio3_ec_diferencial_prueba2(t,x)
Vp=5;
u=Vp*sin(2*pi*t)+5;
xdot = [
x(2, :);
1.776*0.05252*20*gradient(u,t)-10*x(1, :)-7*x(2, :);
];
%[t,x]=ode45('pid_practica9_ejercicio3_ec_diferencial_prueba2',[0,10],[0,0])
% plot(t,x)
but in the solution i get only zeros (and they shouldn't be)
is it possible to work with a derivative in the definition of a diferential equation like i do?
2 个评论
madhan ravi
2018-7-7
Can you post the question to solve?
jose luis guillan suarez
2018-7-7
编辑:Walter Roberson
2018-7-7
回答(1 个)
Walter Roberson
2018-7-7
0 个投票
The t and x values passed into your function will be purely numeric, with t being a scalar and x being a vector the length of your initial conditions (so a vector of length 2 in this case.)
You calculate u from the scalar t value, and you pass the scalar u and scalar t into gradient -- the numeric gradient routine. The numeric gradient() with respect to scalar F and scalar H is always 0.
8 个评论
jose luis guillan suarez
2018-7-8
Walter Roberson
2018-7-9
When you use ode45, the [0 10] tells it that it needs to integrate from t = 0 to t = 10. It will use variable time steps to do so, and it will report at whatever times it wants as long as the first is 0 and the last is 10. If you had specified something like linspace(0,10,101) then it would report with respect to 0:.1:10 but it would still calculate for whatever times it wants.
ode45 is not a fixed timestep solver.
ode45 calls the function you provide with a scalar time, and with a vector of boundary conditions that is the same length as your initial condition. It does not pass all of the times in a single call, because the boundary conditions (x) evolve with time.
jose luis guillan suarez
2018-7-10
Walter Roberson
2018-7-10
> diff(Vp*sin(2*Pi*t)+5, t);
2 Vp Pi cos(2 Pi t)
so if you want u' at time t, then use
2 * pi * Vp * cos(2 * pi * t)
jose luis guillan suarez
2018-7-11
Walter Roberson
2018-7-11
The derivative is 0 except at the integers, and it is undefined at the integers because square() is discontinuous. There are no rising or falling edges for a mathematical square wave.
There are approximations to square waves for which it is meaningful to ask about the derivative. http://mathworld.wolfram.com/FourierSeriesSquareWave.html
jose luis guillan suarez
2018-7-12
jose luis guillan suarez
2018-7-19
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

