Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions in matlab

Tags:

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.

like image 525
yankeefan11 Avatar asked Jun 13 '26 10:06

yankeefan11


2 Answers

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)
like image 73
Oleg Avatar answered Jun 16 '26 12:06

Oleg


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

ans =

    16
like image 37
NPE Avatar answered Jun 16 '26 14:06

NPE