Using inline(), we can define custom functions that perform operations we define.
% lets create a function that takes x as an argument and computes the square root
format compact
func1 = inline('sqrt(x)','x') % the 1st parameter is the function we want and the 2nd is the argument
func1 = Inline function: func1(x) = sqrt(x)
% now we can use it
func1(4)
func1(8)
ans = 2 ans = 2.8284
% we can do more complex functions with more arguments
c = inline('sqrt(a^2+b^2)','a','b')
c = Inline function: c(a,b) = sqrt(a^2+b^2)
% now we can find the hypotenuse
c(4,3)
ans = 5
% first we'll define a custom function
f3 = inline('x^2','x')
f3 = Inline function: f3(x) = x^2
% x must be a symbolic
x = sym('x');
% next, we pass the function to diff()
diff(f3(x),x)
ans = 2*x
% if we are using a pre-defined function, we dont use single-quotes
derivative = inline(diff(f3(x),x),'x')
derivative = Inline function: derivative(x) = x.*2.0
derivative(2*x)
ans = 4*x
% prep, declare symbolic var, declare a function
clear all
x = sym('x');
func = x^3
func = x^3
% we can esily take the derivative
diff(func,x)
ans = 3*x^2
clear all
f1 = inline('x','x')
f1 = Inline function: f1(x) = x
% f1 is simply a linear function
f1(4)
ans = 4
x = sym('x');
integral = int(f1(x),x)
integral = x^2/2
pretty(integral)
2 x -- 2
% the limit of x as x approaches 0
clear all
x = sym('x');
f1 = inline('sin(x)/x','x')
limit(f1(x),x,0)
f1 = Inline function: f1(x) = sin(x)/x ans = 1
clear all
x = sym('x');y = sym('y');
f1 = inline('x^2+2*y^3','x','y')
f1 = Inline function: f1(x,y) = x^2+2*y^3
% partial derivative wrt x
d1 = diff(f1(x,y),x)
d1 = 2*x
% partial wrt y
diff(f1(x,y),y)
ans = 6*y^2