Problems with a script in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter engineer_stud
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around issues encountered while plotting two graphs in MATLAB, specifically focusing on ensuring that the second graph starts where the first one ends. Participants explore the correct implementation of constants and time variables in the script to achieve a continuous plot over the specified time intervals.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their attempt to plot two graphs in MATLAB, noting that the second graph does not start at the expected initial velocity.
  • Another participant suggests replacing the time variable in the second equation to ensure continuity, but the original poster reports that this leads to an incorrect starting value for the second graph.
  • A different participant points out that the initial conditions for the two graphs (u and v0) are different, which may affect the continuity at t=15 seconds.
  • Further suggestions include resetting the time variable and adjusting the plot command to align the second graph correctly with the first.
  • The original poster shares their updated script and describes the behavior of the graphs, indicating that the changes still do not yield the desired outcome.
  • Another participant notes that the values of v0 and u control the curve and that changing them at t=15 causes a discontinuity.
  • Ultimately, a solution is proposed that involves adjusting the time variable for the second plot, which the original poster confirms resolves their issue.

Areas of Agreement / Disagreement

Participants express differing views on how to correctly implement the time and constant variables to achieve the desired graph continuity. While some suggestions lead to improvements, there is no consensus on the initial conditions and their impact on the graph behavior until a final adjustment is confirmed to work.

Contextual Notes

Participants discuss the behavior of the graphs based on specific values of constants and the time variable, indicating that the results are sensitive to these parameters. There are unresolved aspects regarding the initial conditions and their effects on the continuity of the plotted graphs.

Who May Find This Useful

Individuals working with MATLAB for plotting and graphing, particularly those dealing with piecewise functions or transitions between different states in their models.

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 ('');
 
Physics news on Phys.org
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 :)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K