% <function name> = @(<input1>, <input2>, ...) <one_output>
f = @(x, y) x^2 + y;
f(2.0, 0)
f(0, -1)
g = @() rand(); % don't have to have inputs
g()
c = 2.0;
f = @(x) c * x;
f(3.0)
f(5.0)
c = -10.0 % doesn't matter if a change c, the function remembers the old version
f(3.0)
f = @(x) c * x;
f(3.0)
f = @(x, c) c * x;
g = @(x) f(x, 2.0);
g(2.0)
% polynomial coefficients: 3 * x^2 + 1 * x + -2
p = [3, 1, -2];
% use in-built polyval(polynomial_coefficients, x_argument) to evaluate a polynomial at x_argument
polyval(p, 0.0)
polyval(p, 1.0)
figure(1);
x = linspace(-5, 5, 1000);
plot(x, polyval(p, x));
The quad (use in Octave, in Matlab use integral), an example of a slight incompatibility.
$\texttt{quad(f, a, b)}$ integrates function $\texttt{f}$ from $\texttt{a}$ to $\texttt{b}$, but the function has to only take one argument, the x_argument (can be called whatever)
p = [3, 1, -2];
pol = @(t) polyval(p, x); % remember p
quad(pol, -5, 5)
function output_fun = add_c(c)
output_fun = @(x) x + c;
end
add_2 = add_c(2.0);
add_2(5.0)
add_2(-1)
add_234234 = add_c(234234);
add_234234(-234234)