Mysterious tangent line on my plots

I have no idea why, but every time I plot something like this, there is an unwanted straight line that shows up unexpectedly in the graph. Any reason this keeps happening?

5 Comments

This line comes from values of variables, especially dt; You have not soecified dt, D,rho,m, v0, y0. I used some values in my answer. Note the effect of dt on result
@Khalid Mahmood, with respect but you are missing the point. The direction and answer you provided are wrong.
Starnge: my dear you are missing entire idea!!! you still have not provoided the basics of the equations....
The basics are: the initial values and values of D, rho, m, dt....
You must proivide these values before you should eve talk. If you dont provoid ethese values and talk... I am never going to answer you. I wait for values... I will welcome those.
If not provoided, I will never follow your questions
and Fangjun Jiang, how can you say that.... That's very strange... Did you know values of variables before you commented that way? At least I found some values which cause the problem. Furthermore these are variables which are causing this, not MATLAB bult in error
How last value of t ca be zero, i.e t(end)=0? Values of variables are not provoided. This means, this code is incomplete portion of what is causing the problem.environment variables... possibly biggest culprit, clear all, especially t is essential. But after clear all, this incomplete code will not run ... as values of dt, D, rho, etc are not provoided in this code. The section of code that initialises the variables may have logical error. Other possibilities are still there, but I am waiting for values of variables, especially how are those initialised? through a code? Not yet told by Frank

Sign in to comment.

 Accepted Answer

Fangjun Jiang
Fangjun Jiang on 8 Apr 2021
Edited: Fangjun Jiang on 9 Apr 2021
Your code has problem. If you have "clear all" at the begining to clear the variables in the memory, then your code has error.
t has n elements. v has n+1 elements. So plot(t,v) will have error.
The resulting curve is due to whatever data left in t or v.
It is a good practice to pre-allocate memory for your variables.
t=zeros(n,1);
v=zeros(n,1);
and then do your for-loop.

1 Comment

This example code could illustrate the problem.
t=0:155;
v=sin(t*pi/400);
plot(t,v);
t(end)=0;
figure;
plot(t,v);

Sign in to comment.

More Answers (1)

v(1)=2; %initial v=v0=vi
a(1)=9.8; %g
n=40; dt=2.5; %use values like dt 0.1, 0.5 1.1,1.5, 2, 2.5, 5 and see the result
D=1;rho=0.75;area=.2;m=7.5;
s(1)=0;
t(1)=0;
for i=1:n
t(i+1)=dt*(i-1) %t0=0;
v(i+1)=v(i)+a(i)*dt; %vf=vi+at
a(i+1)=9.8-(D*rho*area/(2*m))*(v(i+1)^2);%acceleration
s(i+1)=s(i)+v(i)*dt; %position
end

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!