0% found this document useful (0 votes)
9 views

Matlab coding For BCA II(1)(1)

Uploaded by

magarnabeen86
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Matlab coding For BCA II(1)(1)

Uploaded by

magarnabeen86
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

1.

Limits and Continuity


Here are MATLAB Coding s that cover the topics of Limits and Continuity, including
evaluating limits, handling indeterminate forms, applying algebraic properties of limits,
theorems related to limits of algebraic and transcendental functions, and testing the
continuity of functions.
1.1 Limit of a Function:
In MATLAB Coding, we can use the limit function (from the Symbolic Math Toolbox) to
compute the limit of a function.
MATLAB Coding

syms x; % Define the function


f = (x^2 - 1)/(x - 1); % Compute the limit as x approaches 1
limit_f = limit(f, x, 1);
disp('Limit of (x^2 - 1)/(x - 1) as x approaches 1:');
disp(limit_f);

1.2. Indeterminate Forms:


Indeterminate forms occur when direct substitution leads to expressions like 0/0 or ∞/∞.
We can use MATLAB Coding to handle these cases.
MATLAB Coding
syms x; % Define a function with an indeterminate form
f = sin(x)/x;

% Compute the limit as x approaches 0


limit_f = limit(f, x, 0);
disp('Limit of sin(x)/x as x approaches 0:');
disp(limit_f);

1.3. Algebraic Properties of Limits (without proof):


You can use the basic properties of limits in MATLAB Coding to compute limits of
sums, products, and quotients of functions.
MATLAB Coding
syms x;
f1 = x^2; % Define functions
f2 = 3*x + 2;
sum_limit = limit(f1 + f2, x, 1); % Sum of limits
disp('Limit of (x^2 + (3x + 2)) as x approaches 1:');
disp(sum_limit);

prod_limit = limit(f1 * f2, x, 1); % Product of limits


disp('Limit of (x^2 * (3x + 2)) as x approaches 1:');
disp(prod_limit);

% Quotient of limits
quot_limit = limit(f1 / f2, x, 1);
disp('Limit of (x^2 / (3x + 2)) as x approaches 1:');
disp(quot_limit);

1.4. Limits of Algebraic and Transcendental Functions:


MATLAB Coding can be used to verify the limits of algebraic and transcendental
functions by applying known limit theorems.
MATLAB Coding

syms x;

% Algebraic function: Polynomial


f1 = x^3 - 2*x^2 + x;
limit_f1 = limit(f1, x, 2);
disp('Limit of x^3 - 2x^2 + x as x approaches 2:');
disp(limit_f1);

% Transcendental function: Exponential


f2 = exp(x);
limit_f2 = limit(f2, x, 0);
disp('Limit of exp(x) as x approaches 0:');
disp(limit_f2);

% Transcendental function: Trigonometric


f3 = sin(x)/x;
limit_f3 = limit(f3, x, 0);
disp('Limit of sin(x)/x as x approaches 0:');
disp(limit_f3);

5. Continuity of a Function:
To test continuity, you need to check if a function is continuous at a specific point. If the
left-hand limit, right-hand limit, and the function value at that point are all the same, the
function is continuous at that point.
MATLAB Coding

syms x;
% Define a function
f = piecewise(x < 1, x^2, x >= 1, 2*x + 1);

% Check the left-hand limit at x = 1


left_limit = limit(f, x, 1, 'left');
disp('Left-hand limit as x approaches 1:');
disp(left_limit);

% Check the right-hand limit at x = 1


right_limit = limit(f, x, 1, 'right');
disp('Right-hand limit as x approaches 1:');
disp(right_limit);

% Function value at x = 1
f_at_1 = subs(f, x, 1);
disp('Function value at x = 1:');
disp(f_at_1);

% Check if the function is continuous at x = 1


if left_limit == right_limit && right_limit == f_at_1
disp('The function is continuous at x = 1');
else
disp('The function is not continuous at x = 1');
end
1.6. Types of Discontinuity:
There are different types of discontinuities: removable, jump, and infinite discontinuities.
Here is an of how to identify and classify discontinuities.
MATLAB Coding
syms x
f3 = piecewise(x < 0, x^2, x >= 0, 3*x);
left_limit_f3 = limit(f3, x, 0, 'left');
right_limit_f3 = limit(f3, x, 0, 'right');
f_at_0 = subs(f3, x, 0);
disp('Exercise 3: Testing continuity of the function at x = 0:');
if left_limit_f3 == right_limit_f3 && right_limit_f3 == f_at_0
disp('The function is continuous at x = 0');
else
disp('The function is not continuous at x = 0');
end
Unit 2: Differentiation
It includes the derivative of various functions, inverse functions, special functions
(identity, constant, algebraic functions), trigonometric functions and their graphs,
exponential and logarithmic functions, and composite functions.

2.1. Derivative of Different Functions


You can compute derivatives symbolically using the Symbolic Math Toolbox in matlab
coding. Here's how to differentiate different types of functions.

``` MATLAB Coding


>> syms x;

% Define the functions


f1 = x^3 + 2*x^2 - 5*x + 3; % Cubic function
f2 = x^2 + 4*x + 4; % Quadratic function
f3 = 5*x + 7; % Linear function
f4 = sin(x); % Trigonometric function
f5 = cos(x); % Trigonometric function
f6 = exp(x); % Exponential function
f7 = log(x); % Logarithmic function

% Display Derivate of above functions


disp(['Derivative of f1 is ' char(diff(f1, x))]);
disp(['Derivative of f2 is ' char(diff(f2, x))]);
disp(['Derivative of f3 is ' char(diff(f3, x))]);
disp(['Derivative of f3 is ' char(diff(f3, x))]);
disp(['Derivative of f4 is ' char(diff(f4, x))]);
disp(['Derivative of f5 is ' char(diff(f5, x))]);
disp(['Derivative of f6 is ' char(diff(f6, x))]);
disp(['Derivative of f7 is ' char(diff(f7, x))]);
```

2. Derivative of a Composite Function:


The derivative of a composite function \( f(g(x)) \) can be computed using the chain rule.

``` MATLAB Coding


% Define composite function f(g(x)) where f(x) = x^2 and g(x) = 3x + 1
syms x;

f = x^2;
g = 3*x + 1;

% Chain rule: derivative of f(g(x)) = f'(g(x)) * g'(x)


composite_prime = diff(f, x) * diff(g, x);
disp('Derivative of the composite function f(g(x)) using the chain rule:');
disp(composite_prime);
```

Unit 3: Application of Differentiation:


It covers topics such as the derivatives and slope of curves, increasing and decreasing
functions, convexity of curves, maximization and minimization of functions, marginal
analysis, price and output, competitive equilibrium of firms, and drawing graphs using
first and second-order derivatives.

1. Derivative and Slope of the Curve:


The slope of a curve at a point is given by the derivative of the function at that point. We
can compute and plot the slope of a curve using the derivative.

``` MATLAB Coding


syms x;

% Define a function f(x) = x^3 - 6*x^2 + 9*x


f = x^3 - 6*x^2 + 9*x;

% Compute the first derivative (slope of the curve)


f_prime = diff(f, x);

% Plot the function and its derivative (slope)


x_vals = -2:0.1:5;
f_vals = double(subs(f, x, x_vals));
f_prime_vals = double(subs(f_prime, x, x_vals));

2. Increasing and Decreasing Functions:


A function is increasing when its derivative is positive and decreasing when its
derivative is negative. You can identify intervals of increase and decrease by analyzing
the first derivative.

``` MATLAB Coding


% Identify intervals of increase and decrease
f_prime_vals = double(subs(f_prime, x, x_vals));

% Find where the derivative is positive (increasing) and negative (decreasing)


increasing_intervals = x_vals(f_prime_vals > 0);
decreasing_intervals = x_vals(f_prime_vals < 0);

disp('Increasing intervals:');
disp(increasing_intervals);
disp('Decreasing intervals:');
disp(decreasing_intervals);

3. Convexity of Curves (Concave Up/Down):


A function is convex (concave up) when the second derivative is positive, and concave
(concave down) when the second derivative is negative. We can use the second
derivative to determine convexity.
``` MATLAB Coding
% Compute the second derivative
f_double_prime = diff(f_prime, x);

% Evaluate the second derivative at different points


f_double_prime_vals = double(subs(f_double_prime, x, x_vals));

% Identify convex and concave regions


convex_intervals = x_vals(f_double_prime_vals > 0);
concave_intervals = x_vals(f_double_prime_vals < 0);

disp('Convex intervals:');
disp(convex_intervals);
disp('Concave intervals:');
disp(concave_intervals);

4. Maximization and Minimization of a Function:


To find the local maxima or minima of a function, we need to find the critical points
where the first derivative is zero and check the second derivative (concavity test).

``` MATLAB Coding


Find the critical points (where first derivative is 0)
critical_points = solve(f_prime == 0, x);
disp('Critical points:');
disp(critical_points);

% Compute the second derivative at the critical points to determine maxima or minima
second_derivative_vals = double(subs(f_double_prime, x, critical_points));

% Classify critical points based on second derivative


max_points = critical_points(second_derivative_vals < 0);
min_points = critical_points(second_derivative_vals > 0);

disp('Local maxima points:');


disp(max_points);
disp('Local minima points:');
disp(min_points);

5. Differentiation and Marginal Analysis:


In economics, marginal analysis involves the derivative of a cost or revenue function to
determine the marginal cost or marginal revenue, which helps in decision-making.

``` MATLAB Coding


% Define cost function C(x) = x^2 + 10x + 100
C = x^2 + 10*x + 100;

% Compute the marginal cost (derivative of cost function)


marginal_cost = diff(C, x);

% Evaluate the marginal cost at a specific quantity (e.g., x = 10)


marginal_cost_at_10 = double(subs(marginal_cost, x, 10));
disp('Marginal cost at x = 10:');
disp(marginal_cost_at_10);

6. Price and Output in Competitive Equilibrium:


In competitive markets, firms produce at the point where marginal cost equals marginal
revenue. If we assume the firm's revenue function is \( R(x) = p \cdot x \), where \( p \) is
the price per unit, we can solve for the equilibrium output where marginal cost equals
price.

``` MATLAB Coding


% Define the price function p(x) = 100 - 2x (decreasing price with quantity)
price_function = 100 - 2*x;

% Define the revenue function R(x) = p(x) * x


revenue_function = price_function * x;

% Define the cost function C(x) = x^2 + 10x + 100


cost_function = x^2 + 10*x + 100;

% Marginal revenue (derivative of revenue function)


marginal_revenue = diff(revenue_function, x);

% Marginal cost (derivative of cost function)


marginal_cost = diff(cost_function, x);

% Solve for competitive equilibrium where marginal cost = marginal revenue


equilibrium_output = solve(marginal_cost == marginal_revenue, x);
disp('Competitive equilibrium output:');
disp(equilibrium_output);
Unit 4: Integration and Its Applications:
This unit covers topics like the Riemann Integral, Fundamental Theorem of Calculus
(without proof), techniques of integration, evaluation and approximation of definite
integrals, improper integrals, applications of definite integrals (quadrature, rectification,
volume, and surface integrals), and numerical integration techniques such as the
Trapezoidal and Simpson's rules.
1. Integration
To compute the Riemann integral, we can use the built-in `integrate()` or `integral()`
functions in MATLAB Coding for definite integrals. The Fundamental Theorem of
Calculus states that the derivative of an integral is the original function.

``` MATLAB Coding


syms x;

% Define a function f(x) = x^2 + 2*x + 1


f = x^2 + 2*x + 1;

% Compute the indefinite integral


F = int(f, x);
disp('Indefinite Integral of f(x) = x^2 + 2x + 1:');
disp(F);

% Compute the definite integral from 0 to 2


definite_integral = int(f, x, 0, 2);
disp('Definite Integral of f(x) from 0 to 2:');
disp(definite_integral);

2. Techniques of Integration
In MATLAB Coding, you can perform symbolic integration using different techniques
such as substitution, integration by parts, or partial fraction decomposition.

``` MATLAB Coding


% of Integration by Parts
Syms x
f = x^2 * exp(x);
u = x^2;
v_prime = exp(x);
du = diff(u, x);
v = int(v_prime, x);

% Applying Integration by Parts formula: ∫ u dv = u*v - ∫ v du


integral_by_parts = u*v - int(v*du, x);
disp('Integration by Parts of x^2 * exp(x):');
disp(integral_by_parts);
% of Substitution: ∫ 2x * sqrt(1 + x^2) dx
substitution_integral = int(2*x * sqrt(1 + x^2), x);
disp('Substitution integral of 2x * sqrt(1 + x^2):');
disp(substitution_integral);
or directly, we can perform following code
syms x
f = x^2 * exp(x);
int(f,x)

3. Evaluation and Approximation of Definite Integrals


You can compute definite integrals using the `integral()` function in MATLAB Coding,
which provides numerical methods to approximate integrals.

MATLAB Coding

% Use MATLAB Coding's 'int' function to compute the definite integral from 0 to 2
syms x;
g=x^2;
p = int(g,0,2);
fprintf( 'integration of g is %f \n',p)

4. Improper Integrals
Improper integrals arise when the limits of integration involve infinity or the integrand
has a singularity.

`MATLAB Coding
: Integral of 1/x from 1 to infinity
f = @(x) 1./x;
improper_integral = integral(f, 1, Inf);
disp('Improper Integral of 1/x from 1 to infinity:');
disp(improper_integral);

: Integral of 1/(x^2) from 0 to infinity


f = @(x) 1./(x.^2);
improper_integral = integral(f, 0, Inf);
disp('Improper Integral of 1/x^2 from 0 to infinity:');
disp(improper_integral);
```

### 5. **Applications of Definite Integrals**:

#### 5.1 **Quadrature (Area Under Curve)**:


The area under a curve is given by the definite integral of the function over a given
interval.

``` MATLAB Coding


% Define the function f(x) = x^2
f = @(x) x.^2;

% Compute the area under the curve from 0 to 3


area = integral(f, 0, 3);
disp('Area under the curve f(x) = x^2 from 0 to 3:');
disp(area);
```

5.2 Rectification (Arc Length)


The arc length of a curve \( y = f(x) \) from \( x = a \) to \( x = b \) is given by the integral
of \( \sqrt{1 + (f'(x))^2} \).

``` MATLAB Coding


% Define a function y = x^2
f = @(x) x.^2;

% Compute the derivative of f(x)


f_prime = @(x) 2*x;

% Define the formula for arc length


arc_length = @(x) sqrt(1 + f_prime(x).^2);

% Compute the arc length from x = 0 to x = 2


length = integral(arc_length, 0, 2);
disp('Arc length of y = x^2 from 0 to 2:');
disp(length);
```

5.3 Volume (Solid of Revolution)


The volume of a solid generated by rotating a curve about the x-axis is given by the
integral \( V = \pi \int_{a}^{b} [f(x)]^2 \, dx \).

``` MATLAB Coding


% Define a function f(x) = x^2
f = @(x) x.^2;

% Compute the volume of the solid formed by rotating f(x) about the x-axis from x = 0 to
x=2
volume = pi * integral(@(x) f(x).^2, 0, 2);
disp('Volume of solid formed by rotating y = x^2 from 0 to 2 about the x-axis:');
disp(volume);
```

5.4 Surface Area (Surface of Revolution):


The surface area of a solid generated by rotating a curve about the x-axis is given by
the formula:

\[
A = 2\pi \int_{a}^{b} f(x) \sqrt{1 + (f'(x))^2} \, dx
\]

``` MATLAB Coding


% Define a function y = x^2
f = @(x) x.^2;

% Compute the derivative of f(x)


f_prime = @(x) 2*x;

% Define the formula for surface area


surface_area = @(x) 2*pi*f(x).*sqrt(1 + f_prime(x).^2);

% Compute the surface area from x = 0 to x = 2


area = integral(surface_area, 0, 2);
disp('Surface area of solid formed by rotating y = x^2 from 0 to 2 about the x-axis:');
disp(area);
```

6. Trapezoidal and Simpson’s Rule of Numerical Integration

6.1 Trapezoidal Rule:


The Trapezoidal Rule approximates the integral of a function by dividing the area under
the curve into trapezoids.

MATLAB Coding
% Define a function f(x) = x^2
f = @(x) x.^2;

% Define the interval and number of subintervals (n)


a = 0;
b = 2;
n = 4; % Number of subintervals

% Compute the Trapezoidal Rule approximation


x_vals = linspace(a, b, n+1);
y_vals = f(x_vals);
trap_approximation = (b - a) * (y_vals(1) + 2*sum(y_vals(2:end-1)) + y_vals(end)) /
(2*n);
disp('Trapezoidal Rule Approximation of the integral of x^2 from 0 to 2:');
disp(trap_approximation);
```

6.2 Simpson’s Rule


Simpson's Rule provides a more accurate approximation by fitting parabolas to
segments of the curve.

``` MATLAB Coding


% Define a function f(x) = x^2
f = @(x) x.^2;

% Define the interval and number of subintervals (n, which must be even)
a = 0;
b = 2;
n = 4; % Number of subintervals, should be even

% Compute the Simpson's Rule approximation


x_vals = linspace(a, b, n+1);
y_vals = f(x_vals);
h = (b - a) / n;
simpson_approximation = (h/3) * (y_vals(1) + 4*sum(y_vals(2:2:end-1)) +
2*sum(y_vals(3:2:end-2)) + y_vals(end));

disp('Simpson''s Rule Approximation of the integral of x^2 from 0 to 2:');


disp(simpson_approximation);
Unit 5: Differential Equations, covering topics such as the order and degree of
differential equations, first-order differential equations, and techniques like separable
variables, homogeneous differential equations, and exact differential equations.
1. Differential Equation and Its Order and Degree
The order of a differential equation is the highest derivative present in the equation,
while the degree is the power of the highest order derivative.
MATLAB Coding

% : Define a first-order differential equation y' = y - x


syms y(x) x;
f = diff(y, x) == y - x; % y' = y - x

% Display the order and degree of the equation


order = 1; % Since it's a first-order equation
degree = 1; % Since the highest derivative (y') is to the power of 1

disp('Order of the equation:');


disp(order);
disp('Degree of the equation:');
disp(degree);
2. First-Order Differential Equations of First Degree
A first-order differential equation of the first degree is an equation that involves only the
first derivative and is linear in the first derivative.
: Solve a first-order differential equation of the form dydx=x+y\frac{dy}{dx} = x +
ydxdy=x+y
MATLAB Coding

% Define the first-order differential equation: dy/dx = x + y


syms y(x) x;
ode = diff(y, x) == x + y;

% Solve the differential equation


sol = dsolve(ode);
disp('Solution to the differential equation dy/dx = x + y:');
disp(sol);
3. Differential Equations with Separable Variables
A differential equation is separable if it can be written in the form
dydx=g(x)h(y)\frac{dy}{dx} = g(x) h(y)dxdy=g(x)h(y). The equation can be solved by
separating the variables and integrating both sides.
: Solve dydx=x⋅y\frac{dy}{dx} = x \cdot ydxdy=x⋅y
MATLAB Coding

% Define the separable differential equation: dy/dx = x * y


syms y(x) x;
ode = diff(y, x) == x * y;

% Separate the variables


separated_eqn = ode / y;

% Solve the differential equation


sol = dsolve(ode);
disp('Solution to the separable differential equation dy/dx = x * y:');
disp(sol);
4. Homogeneous Differential Equations
A homogeneous first-order differential equation can be written as
dydx=f(x,y)g(x,y)\frac{dy}{dx} = \frac{f(x, y)}{g(x, y)}dxdy=g(x,y)f(x,y), where both the
numerator and denominator are homogeneous functions of the same degree.
: Solve the homogeneous differential equation dydx=x+yx−y\frac{dy}{dx} =
\frac{x + y}{x - y}dxdy=x−yx+y
MATLAB Coding

% Define the homogeneous differential equation: dy/dx = (x + y)/(x - y)


syms y(x) x;
ode = diff(y, x) == (x + y)/(x - y);
% Solve the homogeneous equation
sol = dsolve(ode);
disp('Solution to the homogeneous differential equation dy/dx = (x + y)/(x - y):');
disp(sol);
5. Exact Differential Equations
A first-order differential equation is exact if it can be written in the form
M(x,y)dx+N(x,y)dy=0M(x, y) dx + N(x, y) dy = 0M(x,y)dx+N(x,y)dy=0, where
∂M∂y=∂N∂x\frac{\partial M}{\partial y} = \frac{\partial N}{\partial x}∂y∂M=∂x∂N.
: Solve the exact differential equation (2xy+y2)dx+(x2+2xy)dy=0(2xy + y^2) dx +
(x^2 + 2xy) dy = 0(2xy+y2)dx+(x2+2xy)dy=0
MATLAB Coding

% Define the exact differential equation: (2xy + y^2) dx + (x^2 + 2xy) dy = 0


syms y(x) x;
M = 2*x*y + y^2; % Coefficient of dx
N = x^2 + 2*x*y; % Coefficient of dy

% Check if the equation is exact (M_y = N_x)


M_y = diff(M, y); % Partial derivative of M with respect to y
N_x = diff(N, x); % Partial derivative of N with respect to x

% Check for exactness


if isequal(M_y, N_x)
disp('The differential equation is exact.');
% Solve the exact equation
sol = dsolve(M*diff(y, x) + N == 0);
disp('Solution to the exact differential equation:');
disp(sol);
else
disp('The differential equation is not exact.');
end
6. Numerical Solution for First-Order Differential Equations
For some differential equations, you may not find an explicit analytical solution, so you
can solve them numerically using MATLAB Coding's built-in functions.
: Solve dydx=x+y\frac{dy}{dx} = x + ydxdy=x+y numerically with initial condition
y(0)=1y(0) = 1y(0)=1
MATLAB Coding

% Define the differential equation dy/dx = x + y


f = @(x, y) x + y;

% Define the initial condition


y0 = 1;
x0 = 0;

% Define the interval of integration


x_span = [0 5];

% Use ode45 to numerically solve the differential equation


[x_vals, y_vals] = ode45(f, x_span, y0);

% Plot the solution


figure;
plot(x_vals, y_vals, '-r');
title('Numerical Solution of dy/dx = x + y');
xlabel('x');
ylabel('y');
grid on;
7. Plotting the Solution of Differential Equations
If you want to plot the solution of the differential equation or visualize the solution
graphically, you can use the following code.
: Plot the solution of dydx=y2−x2\frac{dy}{dx} = y^2 - x^2dxdy=y2−x2
MATLAB Coding

% Define the differential equation dy/dx = y^2 - x^2


f = @(x, y) y^2 - x^2;

% Define the initial condition


y0 = 1;
x0 = 0;

% Define the interval of integration


x_span = [-5 5];

% Use ode45 to solve the differential equation


[x_vals, y_vals] = ode45(f, x_span, y0);

% Plot the solution


figure;
plot(x_vals, y_vals, '-b');
title('Solution of dy/dx = y^2 - x^2');
xlabel('x');
ylabel('y');
grid on;

Unit 6: Computational Methods, covering topics such as Linear Programming


Problems (LPP), the Simplex Method, solving systems of linear equations using various
methods (Gauss Elimination, Gauss-Seidel, and Matrix Inversion), and numerical
methods for solving non-linear equations (Bisection and Newton-Raphson methods).

1 . Solution of LPP by Simplex Method (up to 3 variables)


To solve a linear programming problem using the Simplex method, MATLAB Coding
provides the linprog function. However, we can also solve the problem manually or use
third-party functions for Simplex. Here's an using linprog.
: Solve the following LPP:
Maximize G=3x+2y+z Subject to:
• x+2y+z ≤ 8
• 2x+y+3z ≤10
• x,y,z≥0 MATLAB Coding

% Define the objective function coefficients for maximization


c = [-3, -2, -1]; % Maximize Z = 3x + 2y + z

% Define the constraints: A*x <= b


A = [1, 2, 1; 2, 1, 3]; % Coefficients of the inequalities
b = [8; 10]; % Right-hand side of the inequalities

% Define the bounds on x, y, and z (all variables >= 0)


lb = [0, 0, 0]; % Lower bounds for x, y, and z
ub = []; % No upper bounds

% Use linprog to solve the LPP


[x_opt, negativefval] = linprog(c, A, b, [], [], lb)

3. Solution of System of Linear Equations by Various Methods


3.1 Gauss Elimination Method
MATLAB Coding

% Define the augmented matrix for the system of equations


A = [2, -1, 1; 3, 3, 9; 3, 3, 5]; % Coefficients of the system
b = [3; 12; 7]; % Right-hand side

% Combine A and b into an augmented matrix


augmented_matrix = [A, b];
% Perform Gauss Elimination
n = size(A, 1);
for i = 1:n
% Make the diagonal element 1 by row scaling
augmented_matrix(i, :) = augmented_matrix(i, :) / augmented_matrix(i, i);

% Eliminate the elements below the pivot element


for j = i+1:n
factor = augmented_matrix(j, i);
augmented_matrix(j, :) = augmented_matrix(j, :) - factor * augmented_matrix(i, :);
end
end

% Back substitution
x = zeros(n, 1);
for i = n:-1:1
x(i) = augmented_matrix(i, end) - augmented_matrix(i, 1:n) * x;
end

disp('Solution of the system of equations using Gauss Elimination:');


disp(x);
3.2 Gauss-Seidel Method
MATLAB Coding

% Define the system of linear equations


A = [4, -1, 0, 0; -1, 4, -1, 0; 0, -1, 4, -1; 0, 0, -1, 3];
b = [15; 10; 10; 10];
n = length(b);
x = zeros(n, 1); % Initial guess for the solution
max_iter = 100; % Maximum number of iterations
tol = 1e-6; % Tolerance level

for iter = 1:max_iter


x_old = x; % Store the old solution
for i = 1:n
sum = b(i);
for j = 1:n
if j ~= i
sum = sum - A(i, j) * x(j);
end
end
x(i) = sum / A(i, i); % Update the solution
end
% Check for convergence
if norm(x - x_old, inf) < tol
break;
end
end

disp('Solution of the system of equations using Gauss-Seidel Method:');


disp(x);
3.3 Matrix Inversion Method
MATLAB Coding

% Define the system of linear equations: Ax = b


A = [2, 1, 1; 3, 3, 9; 3, 3, 5]; % Coefficient matrix
b = [3; 12; 7]; % Right-hand side

% Use matrix inversion method to solve Ax = b


x = inv(A) * b;

disp('Solution of the system of equations using Matrix Inversion Method:');


disp(x);

4. Bisection Method for Solving Non-linear Equations


: Solve f(x)=x3−x−2=0f(x) = x^3 - x - 2 = 0f(x)=x3−x−2=0 using the Bisection
method
MATLAB Coding

% Define the function


f = @(x) x^3 - x - 2;

% Define the interval [a, b]


a = 1;
b = 2;

% Define tolerance and maximum iterations


tol = 1e-6;
max_iter = 100;
iter = 0;

% Bisection method
while (b - a) / 2 > tol
c = (a + b) / 2; % Midpoint
if f(c) == 0
break; % Exact solution found
elseif f(a) * f(c) < 0
b = c; % Narrow down the interval
else
a = c;
end
iter = iter + 1;
if iter >= max_iter
disp('Maximum iterations reached');
break;
end
end

disp('Solution found using the Bisection Method:');


disp(c);

5. Newton-Raphson Method for Solving Non-linear Equations


: Solve f(x)=x3−x−2=0f(x) = x^3 - x - 2 = 0f(x)=x3−x−2=0 using Newton-Raphson
method
MATLAB Coding
% Define the function
f = @(x) x^3 - x - 2;

% Define the interval [a, b]


a = 1;
b = 2;
% Define tolerance and maximum iterations
tol = 1e-6;
max_iter = 100;
iter = 0;

% Bisection method
while (b - a) / 2 > tol
c = (a + b) / 2; % Midpoint
if f(c) == 0
break; % Exact solution found
elseif f(a) * f(c) < 0
b = c; % Narrow down the interval
else
a = c;
end
iter = iter + 1;
if iter >= max_iter
disp('Maximum iterations reached');
break;
end
end

disp('Solution found using the Bisection Method:');


disp(c);
5. Newton-Raphson Method for Solving Non-linear Equations
: Solve f(x)=x3−x−2=0f(x) = x^3 - x - 2 = 0f(x)=x3−x−2=0 using Newton-Raphson
method
MATLAB Coding

% Define the function and its derivative


f = @(x) x^3 - x - 2;
f_prime = @(x) 3*x^2 - 1; % Derivative of the function

% Initial guess
x0 = 1.5;
tol = 1e-6;
max_iter = 100;
iter = 0;

% Newton-Raphson method
while abs(f(x0)) > tol
x1 = x0 - f(x0) / f_prime(x0); % Update the guess
x0 = x1;
iter = iter + 1;
if iter >= max_iter
disp('Maximum iterations reached');
break;
end
end

disp('Solution found using the Newton-Raphson Method:');


disp(x0);

You might also like