Getting A blank Graph
조회 수: 3 (최근 30일)
이전 댓글 표시
trying to graph x against y 1,2 and 3 on same graph , getting blank graph.
for x=0:0.01:1
y1=(0.5*x^5)-(0.6*x^4)-(0.6*x^3)+(0.3*x^2)+0.25;
y2=(sin(x)*cos(x))+(x^2)-1;
y3=(5*x*exp(1)^-5*x)-0.2;
end
plot(x,y1)
hold on
plot(x,y2)
hold on
plot(x,y3)
댓글 수: 0
채택된 답변
Torsten
2022년 11월 2일
편집: Torsten
2022년 11월 2일
I changed y3 to what I think you meant.
x=0:0.01:1;
y1=(0.5*x.^5)-(0.6*x.^4)-(0.6*x.^3)+(0.3*x.^2)+0.25;
y2=(sin(x).*cos(x))+(x.^2)-1;
%y3=(5*x*exp(1)^-5*x)-0.2;
y3 = 5*x.*exp(-5*x)-0.2;
hold on
plot(x,y1,'b')
plot(x,y2,'r')
plot(x,y3,'k')
hold off
grid on
댓글 수: 0
추가 답변 (1개)
Davide Masiello
2022년 11월 2일
편집: Davide Masiello
2022년 11월 2일
See below for a correct use of indexing with a for loop
x = 0:0.01:1;
for i = 1:length(x)
y1(i) = (0.5*x(i)^5)-(0.6*x(i)^4)-(0.6*x(i)^3)+(0.3*x(i)^2)+0.25;
y2(i) = (sin(x(i))*cos(x(i)))+(x(i)^2)-1;
y3(i) = (5*x(i)*exp(1)^-5*x(i))-0.2;
end
figure(1)
plot(x,y1)
hold on
plot(x,y2)
hold on
plot(x,y3)
Also consider that this can be done in a much more compact way
x = 0:0.01:1;
y1 = (0.5*x.^5)-(0.6*x.^4)-(0.6*x.^3)+(0.3*x.^2)+0.25;
y2 = (sin(x).*cos(x))+(x.^2)-1;
y3 = 5*x.*exp(-5*x)-0.2 % adjusted probable mistake
figure(2)
plot(x,y1,x,y2,x,y3)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!