2

This looks really simple. I want to define a function:

syms x

f = x^2

I want to be able to do f(4) and it spits out 16. I also want to avoid having to write a new m-file.

2 Answers 2

5

When dealing with symbolic variables, to substitute in a numeric value, use subs(), i.e. symbolic substitution:

syms x
f = x^2

subs(f,4)
Sign up to request clarification or add additional context in comments.

Comments

4
>> f = @(x) x^2;
>> f(4)

ans =

    16

3 Comments

Note that the x in the function handle is NOT the symbolic x you defined.
What is the difference?
You are creating a function on-the-fly which accepts one input, whose name is specified within the ( ). The name itself can be anything and it will not match base workspace variables since each function has it's own workspace when called.

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.