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

8 visualizaciones (últimos 30 días)
Zzahng Cal
Zzahng Cal el 29 de Mzo. de 2022
Comentada: Zzahng Cal el 29 de Mzo. de 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');

Respuesta aceptada

Star Strider
Star Strider el 29 de Mzo. de 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 comentarios
Zzahng Cal
Zzahng Cal el 29 de Mzo. de 2022
i see... thank you so much!!
Star Strider
Star Strider el 29 de Mzo. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Simon Chan
Simon Chan el 29 de Mzo. de 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;

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by