Functions

$f(x) = x^2 -1$

In [30]:
function [f]=func(x)
f=x^2-1;
end
In [31]:
func(2)
ans =  3
In [32]:
function [z1, z2]=func2(x,y)
    z1=x.^2+y.^2;
    z2=x+y;
end
In [33]:
[a, b]=func2(2,3)
a =  13
b =  5
In [34]:
A = [1, 2]; B = [2, 3];
[a1, b1] = func2(A,B)
a1 =

    5   13

b1 =

   3   5

In [35]:
phi = @(x) cosh(x/4);
In [29]:
phi(0)
ans =  1
In [36]:
function f = fact(n)
    f = prod(1:n);
endfunction
In [37]:
fact(5)
ans =  120