0% found this document useful (0 votes)
27 views10 pages

Matlab Oel

The document contains MATLAB code for various numerical methods including Lagrange interpolation, Newton interpolation, and solving a system of equations using an iterative method. It also includes a simulation of a system's response to a cyberattack over time and calculates total transmitted power across an aperture. The code is structured with clear sections for different tasks and outputs results for each computation.

Uploaded by

hawkinshock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

Matlab Oel

The document contains MATLAB code for various numerical methods including Lagrange interpolation, Newton interpolation, and solving a system of equations using an iterative method. It also includes a simulation of a system's response to a cyberattack over time and calculates total transmitted power across an aperture. The code is structured with clear sections for different tasks and outputs results for each computation.

Uploaded by

hawkinshock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

MATLAB OEL AC RABIE YOUNUS 23101013

Q5
CODE

clc;
clear all;
x = [1 2 4 5];
y = [1 4 2 1];

n = length(x);

X = input('Enter interpolation point x = ');


L = 0;

for i = 1:n
term = y(i);
for j = 1:n
if i == j
continue;
end
term = term * (X - x(j))/(x(i) - x(j));
end
L = L + term;
end

fprintf('Interpolated value at x = %f is %f\n', x, L);


xx = linspace(min(x), max(x), 100);
yy = zeros(1, 100);

for k = 1:100
x0 = xx(k);
y0 = 0;
for i = 1:n
term = y(i);
for j = 1:n
if i == j
continue
end
term = term * (x0 - x(j)) / (x(i) - x(j));
end
y0 = y0 + term;
end
yy(k) = y0;
end
plot(x, y, 'o', 'MarkerSize', 8, 'Displayname', 'Data');
hold on;
plot(xx, yy, 'r--', 'Linewidth', 2);
legend
xlabel('X value');
ylabel('Y value');
title('Lagrange Interpolation');
grid on

OUTPUT
FOR X=1

Q2

function yi = NewtonInterp(x,y,xi)
n = length(x);
a = zeros(1,n); % Pre-allocate
a(1) = y(1);
DivDiff = zeros(1,n-1); % Pre-allocate
for i = 1:n-1,
DivDiff(i,1) = (y(i+1) - y(i))/(x(i+1) - x(i));
end
for j = 2:n-1,
for i = 1:n-j,
DivDiff(i,j) = (DivDiff(i+1,j-1) - DivDiff(i,j-1))/(x(j+i) - x(i));
end
end
for k = 2:n,
a(k) = DivDiff(1,k-1);
end
yi = a(1);
xprod = 1;
for m = 2:n,
xprod = xprod*(xi - x(m-1));
yi = yi + a(m)*xprod;
end

Output
Q1

A11 = @(x,y) 4.8;


A12 = @(x,y) 0.5;
A21 = @(x,y) -1.2;
A22 = @(x,y) -1.1;

f1 = @(x,y) 4.8*x + 0.5*y + 0.175;


f2 = @(x,y) -1.2*x - 1.1*y + 0.05;

tol = 1e-3;
kmax = 5;

v = zeros(2, kmax+1);
v(:,1) = [0.05; -0.02];

for k = 1:kmax
xk = v(1,k);
yk = v(2,k);

A = [A11(xk,yk), A12(xk,yk);
A21(xk,yk), A22(xk,yk)];

b = [-f1(xk, yk); -f2(xk, yk)];

if norm(b) < tol


root = v(:,k);
fprintf('Converged at iteration %d\n', k);
break
end

delv = A \ b;
v(:,k+1) = v(:,k) + delv;

if norm(delv) < tol


root = v(:,k+1);
fprintf('Converged at iteration %d\n', k+1);
break
end
end

if ~exist('root','var')
root = v(:,end);
fprintf('Did not converge within %d iterations\n', kmax);
end

disp('Estimated root:');
disp(root);

Q4
h = 0.1;
t = 0:h:10;
n = length(t);

x1 = zeros(1, n);
x2 = zeros(1, n);

x1(1) = 0;
x2(1) = 0;
for i = 1:n-1
dx1 = x2(i);
dx2 = -x2(i) - 2*x1(i) + 5*sin(2*t(i));

x1(i+1) = x1(i) + h*dx1;


x2(i+1) = x2(i) + h*dx2;
end

plot(t, x1, 'b', 'LineWidth', 2)


xlabel('Time (s)')
ylabel('System Vulnerability x(t)')
title('Cyberattack Impact Over Time')
grid on

OUTPUT
Q3

P0 = 1000;
L = 1.2;
a = -L/2;
b = L/2;
n = 98;
h = (b - a) / n;

P = @(x) P0 * (1 - (2*x/L).^2);
x = a:h:b;

S = P(x(1)) + P(x(end));

for i = 2:n
if mod(i-1,3) == 0
S = S + 2 * P(x(i));
else
S = S + 3 * P(x(i));
end
end

TotalPower = (3*h/8) * S;

fprintf('Total transmitted power across aperture: %.4f W\n', TotalPower);

You might also like