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

15 Ansichten (letzte 30 Tage)
Zzahng Cal
Zzahng Cal am 29 Mär. 2022
Kommentiert: Zzahng Cal am 29 Mär. 2022
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');

Akzeptierte Antwort

Star Strider
Star Strider am 29 Mär. 2022
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 Kommentare
Zzahng Cal
Zzahng Cal am 29 Mär. 2022
i see... thank you so much!!
Star Strider
Star Strider am 29 Mär. 2022
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Simon Chan
Simon Chan am 29 Mär. 2022
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;

Kategorien

Mehr zu Graphics finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by