yankeefan11
yankeefan11

Reputation: 485

Functions in matlab

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.

Upvotes: 2

Views: 93

Answers (2)

Oleg
Oleg

Reputation: 10676

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)

Upvotes: 5

NPE
NPE

Reputation: 500883

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

ans =

    16

Upvotes: 4

Related Questions