Info
This question is closed. Reopen it to edit or answer.
While function not stopping at correct point/ how do I get longer vectors for my variables
4 views (last 30 days)
Show older comments
I am trying to plot for y <= 9.8
x(1) = 0;
y(1) = 0;
i = 1;
while y <= 9.8
x(i+1) = x(i) + 0.001;
y(i+1) = y(i) + 10*(exp(-x/4));
i = i +0.1;
end
plot(x,y)
this results in the following graph

and x as a vector [0,1], and y as[0,10]
first, with the formula that I gave for y, y(i) + 10*(exp(-x/4)) when x = 1, should be 7.7880, so the maximum value of x should be greater than 1
second, why did the y value exceed 9.8?
third, how can I fix this so that i 1) I get more values in the vectors and 2) end where y = 9.8?
This is a homework problem, otherwise I would just create a longer vector for x, the answer must be using a while loop
much thanks
1 Comment
Walter Roberson
on 24 Mar 2019
while y <= 9.8
means the same thing as
while all(y <= 9.8)
This is important because your y is a vector.
Your code does not generate the output you show: your code generates an error message about left and right side having different number of elements. In your computation of y(i+1) you use x which is the entire vector x, so the size of the right hand side is going to be the same as the current size of x.
Answers (0)
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!