ploting second derivative from second order differential equationn
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi, Beloow is my code and i am solving second order differential equation numericaly and i am getting plot of first and zeroth derivation from ode45. is there any way to plot the second derivative as well by just calling ode45 ?
function fval = simulation( t,y )
x=y(1);
v=y(2);
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
fval(1,1)=v;
%fval(2,1)= -(c*v+k*x)/m
fval(2,1)=(-c*v-f-e+w+k*x)/m;
end
%%%%main file%%%%%%
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
Y0= [0;3.86];
tspan= [0 1];
options= odeset ('Events', @event);
[tsol,ysol] = ode45(@(t,y) simulation(t,y), tspan, Y0,options);
figure
plot(tsol,ysol(:,1), 'r-');
hold on;
figure
plot(tsol,ysol(:,2), 'g-');
hold on;
figure
Accepted Answer
Jan
on 18 Mar 2021
The easiest solution is to let the function to be integrated accept vectors as input and call it with the time steps and positions replied by ODE45:
[tsol, ysol] = ode45(@(t,y) simulation(t,y), tspan, Y0,options);
dy = simulation(tsol, ysol.').';
d2y = dy(:, 2);
function fval = simulation(t, y)
x = y(1, :);
v = y(2, :);
c = 6850;
m = 450;
k = 0;
f = 2650;
e = 535;
w = 4410;
fval(1, :) = v;
fval(2, :) = (-c * v - f - e + w + k * x) / m;
end
12 Comments
Muhammad Sarmad Zahid
on 18 Mar 2021
Jan there is some syntax error in your code line 2
The 2nd line of my code is empty. So please post the error message, if you get one. Letting me guess which error appears anywhere in the code, is not efficient.
By the way: My code contains only the snippet you have to change to get the 2nd derivative. You have to insert this in your code.
Muhammad Sarmad Zahid
on 19 Mar 2021
oh it was fine thank you for the help jan :)
Muhammad Sarmad Zahid
on 19 Mar 2021
Edited: Jan
on 19 Mar 2021
i have one more question i have this code. I am following you guidance to plot g against time. g is a contant value. How can i plot g ?
Y0= [-0.75 ;0];
tspan= [0 2];
options= odeset ('Events', @freefall);
[tsol,ysol] = ode45(@(t,y) pra(t,y), tspan, Y0,options);
figure
plot(tsol,ysol(:,1), 'r-');
hold on;
figure
plot(tsol,ysol(:,2), 'g-');
hold on;
figure
%%%%function%%%
function fval = pra( t,y )
g=9.8;
y(1);
y(2);
% c=6850;
fval = [y(2); g)];
end
There is a not matching parenthesis in your code in:
fval = [y(2); g)];
You can plot the 2nd component of the output of pra() in exactly the same way, that I've showed you for simulation(): Convert pra() such that it accept vectors as input and use the output of ODE45 to get the wanted values. Afterwards use plot() as for the other components.
Muhammad Sarmad Zahid
on 19 Mar 2021
Edited: Jan
on 19 Mar 2021
i am tring to do that but its not returning me a contant value of g instead it is returning me big values starting from 2.7 to 55 m/s^2. can you please guideme by writing a code? it would be really appreaciat if you check that code as well.
you can chekc the values of f as well
[t,y] = ode45(@(t,y) pra(t,y), [0 1], [-0.75 ; 0 ]);
f=simulation( t,y.').'
%%%%function%%%
function fval = pra( t,y )
g=9.8;
y(1);
y(2);
% c=6850;
fval = [y(2); g.*((t).^0)];
end
Please use the tools to format the code. This makes it easier to read you code. I've done this for you, but it is more efficient, if you do this by your own.
I've showed you how to convert the function simulation() such, that it accepts matrices as input. Then you can use the output of ODE45 to obtain the 2nd derivative also.
Now you have to modify your function pra() accordingly.
These lines are meaningless:
y(1);
y(2);
Instead of:
fval = [y(2); g.*((t).^0)];
you need:
fval = [y(2, :); repmat(g, size(t))];
Using g.*((t).^0) is smart, but the power operation is very expensive. repmat() is more efficient, or alternatively:
fval = [y(2, :); g * ones(size(t))];
Afterwards, do not call "f=simulation( t,y.').'", but:
df = pra(t, y.').';
plot(t, df(:, 2))
Muhammad Sarmad Zahid
on 19 Mar 2021
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in pra (line 10)
fval = [y(2, :); g * ones(size(t))];
Error in practicemailfile (line 20)
df = pra(tsol, ysol.').';
Jan
on 19 Mar 2021
Then call it like this:
df = pra(tsol.', ysol.').';
% ^^
Muhammad Sarmad Zahid
on 20 Mar 2021
it worked. Thank you so much jan
Muhammad Sarmad Zahid
on 20 Mar 2021
Edited: Jan
on 20 Mar 2021
jan kidly guide me on this question as well
Jan
on 20 Mar 2021
But this is the same question again?
More Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)