Problem plotting graph in the same plot

1 view (last 30 days)
Uendi Hajderaj
Uendi Hajderaj on 10 Mar 2019
Answered: Raj on 11 Mar 2019
Hi,
I'm having problems with getting two graphs in the same plot. What I want to plot, is the vector of r in the x axis and e in the y axis and then the upperbound in the y axis. When I run the code, I either get one or the other, but not both.
r=zeros(1, 501);
e=zeros(1, 501);
upperbound=zeros(1, 501);
for R= 0:500
abs(latticepoints(R)-pi*R*R);
r(R+1)=R;
e(R+1)=abs(latticepoints(R)-pi*R*R);
upperbound(R+1)=3*pi*R*R;
end
e;
r;
plot(r,e)
hold on
upperbound;
plot(r, upperbound)
xlabel('values of R')
ylabel(' error')
title ('Plot of the error terms')

Answers (1)

Raj
Raj on 11 Mar 2019
Hi,
Since I dont have the "latticepoints" data, I assumed it as "latticepoints=ones(1,501);"
Now your "FOR" loop starts at R=0 which cannot be correct as "R" is directly used as index for latticepoints and "Subscript indices must either be real positive integers or logicals."
So I changed the "FOR" loop from 1:500 instead of 0:500 and ran the code.
The "hold on" works fine the i got the two graphs in single plot.
You can also remove "hold on" from the script and try plot(r,e,r,upperbound) instead of plotting two times. It will give you the same result.
Let me know your comments.
Thanks.

Tags

Community Treasure Hunt

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

Start Hunting!