Why doesn't my figure(2) display anything?
6 次查看(过去 30 天)
显示 更早的评论
Hi! Badly need help with this one.. I can't get my second figure to display anything ><
%problem: f(x)= -3x^4 + 10x^2 - 3
%one plot for -4 <= x <= 3 and another for -4 <= x <= 4
clc;
x = [-4:1:3];
y = -3*x.^4+10*x.^2-3;
a = [-4:1:4];
b = -3*x.^4+10*x.^2-3;
figure(1)
plot(x,y), xlabel('x ->'), ylabel('y ->'), title('plot for -4 <= x <= 3');
figure(2)
plot(a,b), xlabel('x ->'), ylabel('y ->'), title('plot for -4 <=x <= 4');
0 个评论
采纳的回答
Star Strider
2022-3-29
Either re-define ‘a’ or make ‘b’ a function of ‘a’ instead of ‘x’.
%problem: f(x)= -3x^4 + 10x^2 - 3
%one plot for -4 <= x <= 3 and another for -4 <= x <= 4
clc;
x = [-4:1:3];
y = -3*x.^4+10*x.^2-3;
a = [-4:1:4];
a = linspace(min(a), max(a), numel(x)) % Re-Define 'a' To Be The Same Size As 'x'
b = -3*x.^4+10*x.^2-3;
figure(1)
plot(x,y), xlabel('x ->'), ylabel('y ->'), title('plot for -4 <= x <= 3');
figure(2)
plot(a,b), xlabel('x ->'), ylabel('y ->'), title('plot for -4 <=x <= 4');
.
更多回答(1 个)
Simon Chan
2022-3-29
Variable x has 8 numbers while variable a has 9 numbers.
However, Variable y and b are calculated based on the value of variable x, so they have 8 numbers as well.
When you plot figure(2), you are going to plot variable b vs variable a and they are not the same length. As a result, an error occurs and hence it does not plot anything for you.
Just change the following line and you should be able to get a plot becasue they are now having same length.
b = -3*a.^4+10*a.^2-3;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!