Problem plotting graph in the same plot

조회 수: 1 (최근 30일)
Uendi Hajderaj
Uendi Hajderaj 2019년 3월 10일
답변: Raj 2019년 3월 11일
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')

답변 (1개)

Raj
Raj 2019년 3월 11일
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.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by