MATLAB Answers


How do I plot a function that consists of a variable depending on x?

Asked by Flávio
on 5 Jan 2013

For example, after some calculations to solve my problem, I get y=x^3.

How do I plot that function? I tried to do fplot(@(x) y, limits) but it doesn't work...any solution?



No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 5 Jan 2013

ezplot('x^3', limits)


ezplot(@(x) x.^3, limits)


x = linspace(LowerLimit, UpperLimit, NumberofPoints);
plot(x, x.^3)

Or more generally if you have a symbolic variable y that contains the expression,

f = matlabFunction(y, 'x');

and then

ezplot(f, limits)


x = linspace(LowerLimit, UpperLimit, NumberofPoints);
plot(x, f(x))


Thank you man! It was the matlabFunction that I needed!

Accept this Answer ?

Answer by Jing
on 5 Jan 2013
Edited by Jing
on 5 Jan 2013

Hi, It should be like this:

y=@(x) x^3; fplot(y,[-10,10]);
fplot(@(x) x^3,[-10,10])

You need to define the function handle.

  1 Comment

Thank you;)

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!