Plot a second order equation and plot two equations on the same grafic
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 everyone,
1. I can't plot this function in a grafic, I think the code in the editor is not right. The second order equation is:
(a) dx^2/dt^2 (t) = -xg0*w0^2*sin(wf*t)-w0^2*x-2*M*w0*beta*dx/dt
xg0, w0, M, beta are constants.
2. Plot two solutions in the same grafic.
I already solve other system with ode45, now i want to plot that second order equation with one solution of this sistem.
It is a enginneering problem that consist in a structure with and without a vibration control system. One equation (a) give me the response without that control and the system of equations give me the response with that control... I need to compare that in the same grafic.
Thank's everyone.
Accepted Answer
Kevin Holst
on 8 Feb 2012
You can plot multiple sets of data on one figure multiple ways. The first way would be:
plot(x1,y1,x2,y2,...)
The next most common would involve using hold:
plot(x1,y1)
hold on
plot(x2,y2)
11 Comments
Igor
on 8 Feb 2012
Yes that work but in my case I can't do it like that. Because:
EDITOR:
1. Equation: It is my first problem because I can define in EDITOR the second order equation that I descrive on the first coment
2. Equation system:
s=@(t,y) [y(3); y(4); (-(M+mv*A*L)*xg0*w0^2*sin(wf*t)-M*w0^2*y(1)-2*M*w0*beta*y(3)-mv*A*alfa*L*((mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)))/(M+mv*A*L);(mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)];
[T,Y]= ode45(s,[0 30],[0 0 0 0]);
To plot just the solution of the system I use plot(T,Y(:,1)) (for example). But I want to plot this solution 1 and the second order equation, on the same plot.
Can you help me ?
Kevin Holst
on 8 Feb 2012
So you're wanting to plot s and Y on the same plot? If so, what will s be a function of, t?
fplot is probably what you want to use to get s plotted, however, it only works for functions with one variable. If you can treat y as a constant in your s function for the purposes of this plot I'd suggest doing so. Maybe:
s_ = @(t) s(t,y);
fplot(s_,[0 30])
Igor
on 8 Feb 2012
I can plot all the solutions off the system s, funtion of time (t) with the comand plot(T,Y(:,1)) (for example). What I can't do is define the equation:
dx^2/dt^2 (t) = -xg0*w0^2*sin(wf*t)-w0^2*x-2*M*w0*beta*dx/dt
on the EDITOR and than plot it. After define that funtion, in the end I want to plot the two functions in the same grafic.
I send you my EDITOR:
M=0.577;
mv=1000;
A=1.157*10^-5;
xg0=0.05;
w0=5.17;
wf=5.3;
beta=2/100;
alfa=0.7;
L=0.734;
g=9.81;
delta=0.5;
%Define Equation1 -I CAN'T DEFINE THIS
dx^2/dt^2 (t) = -xg0*w0^2*sin(wf*t)-w0^2*x-2*M*w0*beta*dx/dt ???
%Define System - OK!
s=@(t,y) [y(3); y(4); (-(M+mv*A*L)*xg0*w0^2*sin(wf*t)-M*w0^2*y(1)-2*M*w0*beta*y(3)-mv*A*alfa*L*((mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)))/(M+mv*A*L);(mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)];
[T,Y]= ode45(s,[0 30],[0 0 0 0]);
%PLOT TWO SOLUTIONS - I CAN'T DO THIS
plot (T,Y(:,1), dx^2/dt^2 (t)) ?????
Igor
on 8 Feb 2012
I just can define that first equation with:
r=@(t,y) [y(3); y(4); -xg0*w0^2*sin(wf*t)-w0^2*y(1)-2*M*w0*beta*y(3); 0];
[T,Y]= ode45(r,[0 30],[0 0 0 0]);
Even this way I can plot r and s in the same grafic.. Some help?
Kevin Holst
on 8 Feb 2012
If y is a vector that's constant, I would leave it out of the inputs for the equations that you want to plot. If it's in the workspace, it will be picked up. So your dx^2/dt^2(t) and s equations could probably be written like this for plotting purposes:
dx2dy2_plot = @(t) -xg0*w0^2*sin(wf*t)-w0^2*x-2*M*w0*beta*y(3);
s_plot = @(t) (-(M+mv*A*L)*xg0*w0^2*sin(wf*t)-M*w0^2*y(1)-2*M*w0*beta*y(3)-mv*A*alfa*L*((mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)))/(M+mv*A*L);(mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)
then you can just execute the following plot command
figure
hold on
fplot(s_plot,[0 30])
fplot(dy2dx2_plot,[0 30])
Igor
on 8 Feb 2012
It doesn't work =\. I think I explain something not right.
1. System of equation
s=@(t,y) [y(3); y(4); (-(M+mv*A*L)*xg0*w0^2*sin(wf*t)-M*w0^2*y(1)-2*M*w0*beta*y(3)-mv*A*alfa*L*((mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)))/(M+mv*A*L);(mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)];
[T,Y]= ode45(s,[0 30],[0 0 0 0]);
- This is ok, I can plot the 4 solutions of the system with plot(T,Y(:,1)), solution 1 for example.
2. I define that second order equation and solve with ode45
r=@(t,y) [y(3); y(4); -xg0*w0^2*sin(wf*t)-w0^2*y(1)-2*w0*beta*y(3); 0];
[T,Y]= ode45(r,[0 30],[0 0 0 0]);
3. If the 2. is right, now I want to plot two solution on the same grafic. Isolate I can plot with the command plot(T,Y(:,1)).. But I don't now the command to plot the two. Already try:
plot(T,Y1(:,1),T,Y2(:,1)) , and other things that doesn't work.
I really appreciate the help you given to me, really ;)
Kevin Holst
on 8 Feb 2012
try
[Ts,Ys]= ode45(s,[0 30],[0 0 0 0]);
[Tr,Yr]= ode45(r,[0 30],[0 0 0 0]);
plot(Ts,Ys(:,1),Tr,Yr(:,1))
Igor
on 8 Feb 2012
Worked, finally...thank's to you. Thank you very much.
Just a final question, make sense define and calculate the equation:
dx^2/dt^2 (t) = -xg0*w0^2*sin(wf*t) - w0^2*x - 2*M*w0*beta*dx/dt
with:
r=@(t,y) [y(3); y(4); -xg0*w0^2*sin(wf*t)-w0^2*y(1)-2*w0*beta*y(3); 0];
[T,Y]= ode45(r,[0 30],[0 0 0 0]);
- I say this because it is a system of 4 equations to solve just one. don't now if that make sense.
Tank's.
Kevin Holst
on 8 Feb 2012
Hmm... I don't have too much experience with using ode45, but it looks like you could just solve:
r = @(t) -xg0*w0^2*sin(wf*t)-w0^2*y(1)-2*w0*beta*y(3);
[T,Y] = ode45(r,[0 30],0);
It looks like ode45 solves each equation independently, but I might be wrong about that.
Kevin Holst
on 8 Feb 2012
Also, don't forget to click accept answer under my name up there.
Igor
on 10 Feb 2012
I have some other problem with my dissertation, sorry stop answering. I am a finalist of civil engineering.
All you say it works, thank's to you I have the program correct. Thank you.
I have another question. Now I have two functions in the same graphic. I need to calculate each maximum to calculate the effectiveness of the control system. What is the code for each function?
I send to a reduce EDITOR:
M=0.577;
mv=1000;
A=1.157*10^-5;
xg0=0.05;
w0=5.17;
wf=5.17;
beta=2/100;
alfa=0.7;
L=0.734;
g=9.81;
delta=0.5;
%Equation 1
r=@(t,y) [y(3); y(4); -xg0*w0^2*sin(wf*t)-w0^2*y(1)-2*w0*beta*y(3); 0];
[Tr,Yr]= ode45(r,[0 50],[0 0 0 0]);
%System of equations
s=@(t,y) [y(3); y(4); (-(M+mv*A*L)*xg0*w0^2*sin(wf*t)-M*w0^2*y(1)-2*M*w0*beta*y(3)-mv*A*alfa*L*((mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)))/(M+mv*A*L);(mv*A*alfa*L*((M+mv*A*L)*xg0*w0^2*sin(wf*t)+M*w0^2*y(1)+2*M*w0*beta*y(3))+(M+mv*A*L)*(-mv*A*alfa*L*xg0*w0^2*sin(wf*t)-2*mv*A*g*y(2)-(1/2)*mv*A*delta*abs(y(4))*y(4)))/(mv*A*L*(M+mv*A*L)-(mv*A*L)^2)];
[Ts,Ys]= ode45(s,[0 50],[0 0 0 0]);
%Graphic
plot(Ts,Ys(:,1),Tr,Yr(:,1))
% Effectiveness of the system ????
Tank's.
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
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)