Problems with a script in MATLAB

  • Topic: MATLAB 
  • Thread starter engineer_stud
  • Start date
  • Tags Tags
    Matlab
6 replies · 3K views · last reply
engineer_stud
Messages
8
Reaction score
0
Hi Guys!

I am trying to plot two different graphs in the same window using MATLAB. I have some constants that I declare, I then plot the graph from 0 to 15 seconds. Then I change two constants and plot another graph from 15 to 30. The units along the x-axis is Time (seconds) and velocity along (m/s) along the y axis.

The first graph seem to come out right, but the second is not. Don't know what is wrong. I believe I have written the function correctly.

This is my script :

clear all;
m = 200;
k = 100;
v0 = 0;
u = 200;

t = linspace(0,15,1500);
v = exp((-k/m)*t)*(v0-(u/k))+u/k;

figure(3);clf(3);
plot (t,v);
grid on;
hold on;

u = 0;
v0 = 2;
t = linspace(15,30,1500);
v = exp((-k/m)*t)*(v0-(u/k))+u/k;
plot(t,v);
ylabel('');
ylim([0 3]);
xlim([0 30]);
xlabel('');
title ('');
 
so you want to plot the second plot over the first and its instead plotting it next to the first?

if so then maybe in the second v equation you replace t by (t-15)
 
jedishrfu said:
so you want to plot the second plot over the first and its instead plotting it next to the first?

if so then maybe in the second v equation you replace t by (t-15)
Yes, I tried that before I posted the question. The problem then is that the graph start at 1 and not 2. The initial value for v0 is set equal to 2 and not 1.

I am expecting the second graph to start where the first one finished. It should be continuous.

This is how I implement your solution :

v = exp((-k/m)*(t-15)*(v0-(u/k))+u/k);
 
okay the first graph has u=200 and v0=0 whereas the second has u=0 and v0=2

So I say its working as it should when I made the u=200 and v0=0 in the second part the graph extended as expected.
 
Hmm, weird. Just to be sure, this is the script I have :

clear all;
m = 200;
k = 100;
v0 =0;
u = 200;

t = linspace(0,15,1500);
v = exp((-k/m)*t)*(v0-(u/k))+u/k;

figure(4);clf(4);
plot (t,v);
grid on;
hold on;

u = 0;
v0 = 2;
t = linspace(15,30,1500);
v = exp((-k/m)*(t-15)*(v0-(u/k))+u/k);
plot(t,v);
ylim([0 2.5])
ylabel('Velocity [m/s]');
xlabel('Time [sec]');
title ('Title');

It gives me a graph that grows exponentially from 0 to 15 seconds, and reaches a constant velocity of 2 m/s. Now that velocity will be the new initial velocity (v0) equal to 2 m/s. The u will now be 0 N. When I change these constants and plot again with t = t-5 as suggested the second graph gets a jump from 2 to 1 (on the y axis). The graph starts at v = 1 m/s and decays exponentially toward 30 seconds. It shouldn't start at 1, it should start at 2

Anyways, I will try on a different computer with MATLAB on it. Btw I am using MATLAB R2012b

Thanks for your help.
 
Okay I am using MATLAB 2011 on a Mac and the graph I see is an exponential that tapers off at 15 and then a horizontal line at zero.

I look at the variables and they represent the graph I see.

It looks like the v0 and u values control the curve and changing them at 15 causes the break.

Were you expecting both curves to have the same value at t=15? Thats clearly not the case.

I think when you reset the v0 and the u at t=15 then you have to reset the time t too.

and perhaps padjust the plot to be plot(t+15,v)

as shown below:

clear all;
m = 200;
k = 100;
v0 = 0;
u = 200;

t = linspace(0,15,1500);
v = exp((-k/m)*t)*(v0-(u/k))+u/k;

figure(3);clf(3);
plot (t,v);
grid on;
hold on;

u = 0;
v0 = 2;
t = linspace(0,15,1500);
v = exp((-k/m)*t)*(v0-(u/k))+u/k;
plot(t+15,v);
ylabel('');
ylim([0 3]);
xlim([0 30]);
xlabel('');
title ('');
 
Last edited:
Yes, that did the trick. Now the graph looks like the way I wanted it.

Thanks :)