I have read several people asking about why their matlab wont plot anything. I still can't figure out why mine wont plot. This is my code
>> close all
>> n=1:25;
>> R(n) = -80;
>> while n<26
R(n+ 1) = -0.8*R(n);
end
plot(n,R(n))
once I type end and press enter, the hashmark showing exactly where you are typing disappears and stops blinking so I feel like my plotting isn't going through. I don't know if that has anything to do with it. Another thing I notice is that why it shows >> for previous statements and once I enter the while n<26 line, it doesn't give me anymore >> lines. I am very inexperienced with Matlab, any help is highly appreciated. I've read figure() helps some people, where exactly in my code am I supposed to type this?
Also, when I close my matlab it says "an operation is in progress....." I don't know if this matters.

6 Comments

Well for one, your while loop is infinite. n is "less than" 26 always, and you don't ever change its value.
Thanks for your response, I went with 26 because I want 1-25 to go through and I dont know how to put less than or equal.. but you're right its always less than 26.
If you tell me what you would like the code to do I'd be glad to help you fix your code!
Are you trying to set the values in R(n) = -.8*previous value? Or only multiply some of them by -.8 or what is your goal?
This is my assignment for class, see attachment. The code I am using is the one my friend use and I was playing with it, however I am not getting a plot so thats my main issue.
So your problem provides that R(1) = -80, and you need to compute R(2 through 25) and then plot R.
The first step is similar to what you have,
R(1) = -80;
but now you need to loop through the remaining 24 values.
As Walter Roberson described below, try thinking of how a for loop can be used to solve this problem.
Will do, thanks!

Sign in to comment.

 Accepted Answer

You have
n=1:25
while n<26
R(n+ 1) = -0.8*R(n);
end
After the first statement, n is a vector of 25 values. The n<26 in the next line is then comparing that vector of 25 values to the value 26. It happens that all of them are less than 26 so the result of the < is a vector of 25 "true" (1) values. When you use "if" or "while" with a vector or array of values, "if" or "while" consider the condition to be true if all of the values are non-zero, which they are in this case (being all true which is 1). So the test in the while is satisfied and the loop body is entered.
In the first execution of the loop body, since n is 1:25, you are doing the equivalent of
R((1:25)+1) = 0.8*R(1:25)
which is
R(2:26) = 0.8*R(1:25)
you have 25 values on the right side and 25 output locations on the left, so the assignment is well defined. This will extend R (which you defined as length 25) to being length 26. This will not change n in any way.
You then reach the end of the while loop, and you go back up to the test while n<26. n has not changed at all, so the test is still true, and R(2:26) = 0.8*R(1:25) is done again. And then it loops back and n has not changed so n<26 is still completely true and R(2:26) = 0.8*R(1:25) is done again, and so on. n never changes so n<26 never changes and the loop never ends. You never get back the >> prompt because the "while" is still executing.
You should be using a for loop:
R = zeros(1,26);
R(1) = -80;
for n = 1 : 25
R(n+ 1) = -0.8*R(n);
end

3 Comments

Thanks for your response! That was great information and I completely understand my mistake. I am still not experienced enough to know how to go about fixing it but I will definitely keep trying. So assuming I try your code, how would I plot that?
Worked like a charm. Thanks so much for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!