using gradient with ode45
Show older comments
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 Comments
madhan ravi
on 7 Jul 2018
Can you post the question to solve?
jose luis guillan suarez
on 7 Jul 2018
Edited: Walter Roberson
on 7 Jul 2018
Answers (1)
Walter Roberson
on 7 Jul 2018
0 votes
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 Comments
jose luis guillan suarez
on 8 Jul 2018
Walter Roberson
on 9 Jul 2018
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
on 10 Jul 2018
Walter Roberson
on 10 Jul 2018
> 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
on 11 Jul 2018
Walter Roberson
on 11 Jul 2018
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
on 12 Jul 2018
jose luis guillan suarez
on 19 Jul 2018
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
