Error with plotting my function

Having issues getting my nonlinear function to plot.
f = @(x) 2.^x-5*x+2; %Given Function
xaxis = [0:0.5:2]; %x-axis
plot(xaxis,f) %plotting function //DOESN'T WORK

2 Comments

"Doesn't work" does not explain the problem. You get an error message:
Error using plot
Invalid data argument.
This is an important hint. The 2nd input is a function handle, but Matlab expect numerical values.
For a solution see Star Strider's answer.
yeah sorry about that. This is my first time using a forum like this and I'm still relatively new to coding. Thanks for insight!

Sign in to comment.

 Accepted Answer

It works if you give ‘f’ a vector to work with
f = @(x) 2.^x-5*x+2; %Given Function
xaxis = [0:0.5:2]; %x-axis
plot(xaxis,f(xaxis)) %plotting function //DOESN'T WORK —> YEs, IT DOES!
Experiment to get different results.
.

2 Comments

ahhhh I see the issue now, thanks for help!!
My pleasure!
.

Sign in to comment.

More Answers (0)

Products

Release

R2021a

Tags

Community Treasure Hunt

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

Start Hunting!