Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

practicals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

PROGRAM-10

WAP to implement Newton’s Divided Difference Interpolation Formula.


Source Code:
function divided_diff = dividedDifferenceTable(x, y)
n = length(x);
divided_diff = zeros(n, n);
divided_diff(:, 1) = y'; % First column is y values
for j = 2:n
for i = 1:(n-j+1)
divided_diff(i, j) = (divided_diff(i+1, j-1) - divided_diff(i, j-1)) / (x(i+j-1) - x(i));
end
end
end
function result = newtonInterpolation(x, y, value)
n = length(x);
divided_diff = dividedDifferenceTable(x, y);
coeff = divided_diff(1, :);
result = coeff(1);
product_term = 1;
for i = 2:n
product_term = product_term * (value - x(i-1));
result = result + coeff(i) * product_term;
end
end
x = [1, 2, 3, 4]; % Example x values
y = [1, 8, 27, 64]; % Example y values (e.g., y = x^3)
value_to_interpolate = 2.5; % Example value to interpolate
fprintf('Divided difference table:\n');
disp(dividedDifferenceTable(x, y));
interpolated_value = newtonInterpolation(x, y, value_to_interpolate);
fprintf('Interpolated value at x = %.2f: %.4f\n', value_to_interpolate, interpolated_value);

Output:
PROGRAM-11
WAP to implement Lagrange’s Interpolation Formula.
Source Code:
% Function to perform Lagrange interpolation
function result = lagrangeInterpolation(x, y, value)
n = length(x);
result = 0;
for i = 1:n
L = 1;
for j = 1:n
if j ~= i
L = L * (value - x(j)) / (x(i) - x(j));
end
end
result = result + y(i) * L;
end
end
x = [1, 2, 3, 4]; % Example x values
y = [1, 8, 27, 64]; % Example y values (e.g., y = x^3)
value_to_interpolate = 2.5; % Example value to interpolate
interpolated_value = lagrangeInterpolation(x, y, value_to_interpolate);
fprintf('Interpolated value at x = %.2f: %.4f\n', value_to_interpolate, interpolated_value);

Output:
PROGRAM-12
WAP to implement Numerical Integration using Trapezoidal Rule.
Source Code:
% MATLAB code to implement Numerical Integration using the Trapezoidal Rule
% Function to perform numerical integration using the Trapezoidal Rule
function result = trapezoidalRule(f, a, b, n)
h = (b - a) / n;
x = a:h:b;
y = f(x);
% Apply the Trapezoidal Rule
result = (h / 2) * (y(1) + 2 * sum(y(2:end-1)) + y(end));
end
% Main script
f = @(x) x.^2; % Example function (e.g., f(x) = x^2)
a = 0; % Lower limit of integration
b = 2; % Upper limit of integration
n = 100; % Number of subintervals
result = trapezoidalRule(f, a, b, n);
% Print output
fprintf('Method used: Trapezoidal Rule\n');
fprintf('The numerical integration result from %.2f to %.2f is: %.4f\n', a, b, result);

Output:
PRACTICAL – 11
Aim: To implement CFG with single symbol.
Let the CFG be : S -> aSbA|b , A-> Sa|a.

Result:
PRACTICAL – 12
Aim: To implement CFG with multiple symbols.
Let the production rules CFG be:

Result:
PRACTICAL – 13
Aim: To implement Regular Expressions.

Let the regular expression be: (a+b)*ab

Result:
PRACTICAL – 14
Aim: To implement Regular Pumping Lemma.

Checking if the language is regular or not:


Result:
PRACTICAL – 15
Aim: To implement Context Free Pumping Lemma.

Checking if the language is context free or not:


Result:
PRACTICAL – 16
Aim: To implement CFG to CNF transformation.
Let the production rules CFG be:

Result:

You might also like