Getting A blank Graph

1 回表示 (過去 30 日間)
Patrick
Patrick 2022 年 11 月 2 日
編集済み: Davide Masiello 2022 年 11 月 2 日
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)

採用された回答

Torsten
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

その他の回答 (1 件)

Davide Masiello
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
y3 = 1×101
-0.2000 -0.1524 -0.1095 -0.0709 -0.0363 -0.0053 0.0222 0.0466 0.0681 0.0869 0.1033 0.1173 0.1293 0.1393 0.1476 0.1543 0.1595 0.1633 0.1659 0.1674 0.1679 0.1674 0.1662 0.1641 0.1614 0.1581 0.1543 0.1500 0.1452 0.1401
figure(2)
plot(x,y1,x,y2,x,y3)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by