1.
Calculate Factorial Using factorial
n = 5;
f = factorial(n);
disp(f)
2. Create Anonymous Functions
f = @(x) x.^2 + 2*x + 1;
disp(f(3))
3. Anonymous Functions with Multiple Inputs
f = @(x,y) x.^2 + y.^2;
disp(f(3,4)) % Outputs 25
4. Function Vectorization Using arrayfun
f = @(x) x^2 + 2*x + 1;
v = 1:10;
y = arrayfun(f, v)
5. Function Handle Arrays
funcs = {@sin, @cos, @tan};
x = pi/3;
results = cellfun(@(f) f(x), funcs);
disp(results)
5. Solve Linear Systems Using Backslash
A = [3 2; 1 4];
b = [5; 6];
x = A \ b;
disp(x)
6. Solving Equations
eqn = x^2 + 5*x + 6 == 0;
sol = solve(eqn, x);
disp(sol)
Solve numerically with fzero:
f = @(x) x^3 - 2*x - 5;
root = fzero(f, 2); % initial guess 2
disp(root)
7. Numerical Integration
f = @(x) exp(-x.^2);
q = integral(f, 0, 1);
fprintf('Integral = %.4f\n', q);
8. Numerical Differentiation
x = linspace(0, 2*pi, 100);
y = sin(x);
dy = diff(y) ./ diff(x); % approximate derivative
plot(x(1:end-1), dy);
title('Numerical Derivative of sin(x)');
9. Basic Symbolic Math (with Symbolic Toolbox)
syms x
f = x^3 + 2*x^2 - x + 5;
df = diff(f, x);
intf = int(f, x);
disp(df)
disp(intf)
10. Root Finding with fsolve
fun = @(x) x^3 - 2*x - 5;
x0 = 2; % initial guess
root = fsolve(fun, x0);
disp(root)
11. Using fzero to Find Roots Numerically
fun = @(x) x^3 - 2*x - 5;
root = fzero(fun, 2);
disp(root)
12. Use trapz for Numerical Integration
x = 0:0.01:1;
y = x.^2;
integral_val = trapz(x,y);
fprintf('Integral = %.4f\n', integral_val)
13. Using interp1 for 1D Interpolation
x = 0:10;
y = sin(x);
xq = 0:0.1:10;
yq = interp1(x,y,xq,'spline');
plot(x,y,'o',xq,yq,'-')
14. Use ode45 to Solve Differential Equations
odefun = @(t,y) -2*y + sin(t);
[t,y] = ode45(odefun, [0 10], 0);
plot(t,y);
xlabel('Time');
ylabel('Solution y(t)');
15. Numerical Solution of ODEs (Ordinary Differential Equations)
Solve ODE using ode45
% dy/dt = -2*y, y(0)=1
odefun = @(t,y) -2*y;
tspan = [0 5];
y0 = 1;
[t,y] = ode45(odefun, tspan, y0);
plot(t, y)
xlabel('Time'); ylabel('y(t)')
title('Solution of dy/dt = -2y using ode45')
16. Nonlinear Optimization
Minimize function using fminunc
fun = @(x) (x(1)-1)^2 + (x(2)-2)^2;
x0 = [0,0];
options = optimoptions('fminunc','Display','iter');
[x,fval] = fminunc(fun, x0, options);
fprintf('Min at x = (%.2f, %.2f), fval = %.2f\n', x(1), x(2), fval);
17.Guss Shield
A=input('Enter a co-efficient Matrix A: ');
B=input('Enter a source vector B: ');
C=input('Enter initial gauss vector: ');
n=input('Enter no of iterations: ');
e=input('Enter your tolerance: ');
N=length(B);
X=zeros(N,1);
Y=zeros(N,1); %for stopping criteria
for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*C([1:i-1,i+1:N]))/A(i,i);
C(i) = X(i);
end
fprintf('iteration no%d\n',j)
X
if abs(Y-X)<e
break
end
Y=X;
End
Input
A = [4 -1 0; -1 4 -1; 0 -1 3];
B = [15; 10; 10];
initial guess = [0; 0; 0];
n = 25;
e = 1e-4;
18. Jacoobi
A=input('Enter a co-efficient Matrix A: ');
B=input('Enter a source vector B: ');
C=input('Enter initial gauss vector: ');
n=input('Enter no of iterations: ');
e=input('Enter your tolerance: ');
N=length(B);
X=zeros(N,1);
for j=1:n
for i=1:N
X(i)=(B(i)/A(i,i)) - (A(i,[1:i-1,i+1:N])*C([1:i-1,i+1:N]))/A(i,i);
fprintf('iteration no%d\n',j)
end
if abs(X-C)<e
break
end
C=X;
End
19.Newton Raphson Method
clc;
clear all;
close all;
f= input('enter your function:');
df= input('enter derivatives of this function:');
e= input('enter tolerance:');
x0 = input('enter intial guess:');
n = input('enter no of iteration:');
if df(x0)~=0
for i = 1 : n
x1=x0-f(x0)/df(x0);
fprintf('X%d=%.20f\n',i,x1)
if abs(x1-x0)<e
break
end
x0 = x1;
end
else
disp('Newton Raphson method faield');
end
f= @(x) 2^x -5*x +2;
df= @(x) log(2)*(2^x)-5;
e= 10^-4
x0 = 0;
n = 10;
20. Lagrange Interpolation
function y_interp = lagrange_interp(x_data, y_data, x_interp)
% Lagrange interpolation
% x_data: vector of x points
% y_data: vector of y points
% x_interp: the point at which to evaluate the interpolation
n = length(x_data);
y_interp = 0;
for i = 1:n
% Initialize the Lagrange basis polynomial L_i
L = 1;
for j = 1:n
if j ~= i
L = L * (x_interp - x_data(j)) / (x_data(i) - x_data(j));
end
end
% Add the term to the total interpolation
y_interp = y_interp + y_data(i) * L;
end
end
% Sample data points
x = [1 2 4];
y = [1 4 2];
% Interpolate at x = 3
x_val = 3;
y_val = lagrange_interp(x, y, x_val);
or
% Sample input (you can replace with your own input code)
x_data = [1 2 4]; % x values
y_data = [1 4 2]; % f(x) values
x_interp = 3; % Point to interpolate
n = length(x_data);
y_interp = 0;
for i = 1:n
% Initialize the Lagrange basis polynomial L_i
L = 1;
for j = 1:n
if j ~= i
L = L * (x_interp - x_data(j)) / (x_data(i) - x_data(j));
end
end
% Add the term to the total interpolation
y_interp = y_interp + y_data(i) * L;
end
fprintf('Interpolated value at x = %.4f is y = %.6f\n', x_interp, y_interp);
21. Simpson's 1/3 Rule for Numerical Integration
% Input
a = input('Enter lower limit (a): ');
b = input('Enter upper limit (b): ');
n = input('Enter number of subintervals (even number): ');
% Ensure n is even
if mod(n, 2) ~= 0
error('Number of subintervals must be even.');
end
h = (b - a) / n; % Step size
% Define the function to integrate
f = @(x) x^2 + 3*x + 2; % Example function: change as needed
% Apply Simpson's 1/3 Rule
x = a:h:b;
y = arrayfun(f, x);
sum_odd = sum(y(2:2:end-1));
sum_even = sum(y(3:2:end-2));
I = (h/3) * (y(1) + 4*sum_odd + 2*sum_even + y(end));
% Output
fprintf('Approximate integral using Simpson''s 1/3 Rule is: %.6f\n', I);
If you integrate f(x)=x2+3x+2f(x) = x^2 + 3x + 2f(x)=x2+3x+2 from 0 to 4 with 4 intervals:
Input:
a=0
b=4
n=4
Output:
Approximate integral using Simpson's 1/3 Rule is: 58.666667
22. Curve Fit
x=[1:10];
y=[5,8,6,12,15,6,7.5,8,16,8];
cftool
or
clc;
clear all;
close all;
X= input('Enter list of abscissas: ');
Y=input('Enter list of ordinates: ' );
F = input('Enter 1 for linear fit, 2 for probabolic and so on: ');
n =legth (X);
N = F+1;%dimension for A matrix
A =zeros(N,N);
for i = 1:N
for j = 1:N
A(i,j)=sum (X.^(i+j-2));
end
end
B =zeros(N,1);
for k = 1:N
B(k)=sum ((X.^(k-1)).*Y);
end
U=A\B
disp('Your polynomial is:')
for k = -1:2
fprintf('+(%.2f)\n',u(k),k-1)
end
fprintf('+(%.2f)\n',F(1))
%plotting
P= flip(u)
x= linespace(X(1),x(n),100)
y=polyval(P,x);
plot(x,y,'r')
hold on
plot(x,y,'0')
A = polyval(P,P0);
fprintf('Approximate value at given data point is',u(k),k-1)