Why doesn't my figure(2) display anything?
9 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 29 Mar 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'
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 Comments
More Answers (1)
Simon Chan
on 29 Mar 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;
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!