1

The explicit problem is as follows: Write a function and use the fplot command to plot f(x)=x^3-x+8 versus x, for values of x from 0 to 100.

The easy way, throw the following code into matlab and call it a day:

Testing = @(x) x^3 - x + 8
fplot(Testing, [0 100])

and done!

but I want to take a slightly more 'difficult' approach. Basically I made a seperate .m file that has the following:

function [y] = blah(x)
y = (x^3) - x + 8;
end

and a script with the following

fplot('blah', [0 100])

but it doesn't work. whyyyy ( i keep getting a positive-sloped line)

Thanks!

3
  • I tried it on my pc and both are giving the same results!, so what is the problem ? Commented Dec 6, 2017 at 9:40
  • You need an @ with your call to that function. See the duplicate target and gnovice's comment on the accepted answer Commented Dec 6, 2017 at 10:22
  • Possible duplicate of MATLAB odd 'Too many input arguments' error Commented Dec 6, 2017 at 10:23

1 Answer 1

0

In my version of Matlab (2016b), it works when blah is not a string:

blah = @(x) (x.^3) - x + 8;

plot(0:100, blah(0:100),'r.-')
hold all
fplot(blah,[0 100],'kx-')
Sign up to request clarification or add additional context in comments.

3 Comments

This is not what the OP is asking. The OP is asking why it does not work with function [y] = blah(x)
@SardarUsama, issue is the same whether blah is in an independent file or an inline function. fplot plots x = 1:100 and y = 1:100 in both cases. Using the function handle solves the issue.
The OP already knows that it works with blah = @(x) (x.^3) - x + 8; and clearly is not asking what you have answered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.