Why doesn't my figure(2) display anything?

조회 수: 8 (최근 30일)
Zzahng Cal
Zzahng Cal 2022년 3월 29일
댓글: Zzahng Cal 2022년 3월 29일
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');

채택된 답변

Star Strider
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'
a = 1×8
-4.0000 -2.8571 -1.7143 -0.5714 0.5714 1.7143 2.8571 4.0000
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');
.
  댓글 수: 2
Zzahng Cal
Zzahng Cal 2022년 3월 29일
i see... thank you so much!!
Star Strider
Star Strider 2022년 3월 29일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by