# Problems with a script in MATLAB

• MATLAB
• engineer_stud
In summary, the first graph is correct, but the second graph does not match. The first graph seems to come out right, but the second is not. The problem is that the second graph starts at 1 instead of 2. The solution is to change v0 and u at t=15.
engineer_stud
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

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 :)

## What is MATLAB and what is a script?

MATLAB is a programming language commonly used in scientific and engineering fields for data analysis, mathematical computations, and creating visualizations. A script in MATLAB is a file that contains a series of commands and functions that can be executed in a sequential manner.

## What are some common problems that can occur in a MATLAB script?

Some common problems that can occur in a MATLAB script include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code is not written in the proper format, logical errors occur when the code does not produce the desired output, and runtime errors occur when the code encounters an unexpected issue during execution.

## How can I debug a MATLAB script with errors?

To debug a MATLAB script, you can use the built-in debugging tools such as the MATLAB debugger, breakpoints, and the "dbstop" function. You can also use the "try-catch" block to catch and handle errors.

## What should I do if my MATLAB script is running slowly?

If your MATLAB script is running slowly, you can try optimizing your code by vectorizing operations, preallocating arrays, and avoiding unnecessary loops. You can also check for any memory leaks or consider using parallel computing techniques to speed up the execution.

## Are there any resources available for troubleshooting MATLAB script problems?

Yes, there are many resources available for troubleshooting MATLAB script problems. The official MATLAB documentation, online forums, and user communities are great places to start. Additionally, MATLAB also has a help desk where you can submit your questions and get assistance from their technical support team.

Replies
4
Views
1K
Replies
8
Views
647
Replies
1
Views
1K
Replies
1
Views
3K
Replies
10
Views
2K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
4
Views
1K
Replies
1
Views
1K