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

Python

The document contains various MATLAB scripts for solving mathematical problems, including Fourier series, matrix methods, Laplace transforms, and eigenvalue calculations. Each section prompts the user for input, processes the data, and displays the results graphically or numerically. The scripts utilize symbolic computation and numerical methods to solve differential equations and analyze matrices.

Uploaded by

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

Python

The document contains various MATLAB scripts for solving mathematical problems, including Fourier series, matrix methods, Laplace transforms, and eigenvalue calculations. Each section prompts the user for input, processes the data, and displays the results graphically or numerically. The scripts utilize symbolic computation and numerical methods to solve differential equations and analyze matrices.

Uploaded by

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

FOURIER SERIES -- 1

clear all

close all

clc

syms t

% THE EQUATION IN THE QUESTION SHOULD BE INSERTED HERE

f = input("Enter the func on: ");

I = input("Enter the intervals: ");

m = input("Enter the harmonics: ");

a = I(1);

b = I(2);

L = (b - a) / 2;

a0 = int(f, a, b) / L;

Fx = a0 / 2;

for n = 1:m

figure;

an(n) = int(f * cos(n * pi * t / L), a, b) / L;

bn(n) = int(f * sin(n * pi * t / L), a, b) / L;

Fx = Fx + an(n) * cos(n * pi * t / L) + bn(n) * sin(n * pi * t / L);

Fx = vpa(Fx, 4);

fplot(Fx, [a, b]);

hold on

fplot(f, [a, b]);

tle(['Fourier series ', num2str(n), ' harmonics']);

legend('Fourier series', 'Func on plot');

hold off

end

disp(strcat('Fourier series with ', num2str(n), ' harmonics is:'));

disp(Fx)
PAGE RANKING== 2
clear all

clc

% THE EQUATION (MATRIX) IN THE QUESTION SHOULD BE INSERTED HERE

A = input('Enter the value of the matrix: ');

[V, D] = eig(A);

for i = 1:length(A)

if round(D(i, i), 10) == 1

X = V(:, i);

break;

end

end

[Y, I] = sort(X, 'descend');

pn = ('A':'E');

pn = pn(I);

disp(pn)
METHOD OF VARIATION == 3

clear all

close all

clc

syms c1 c2 x m

% THE EQUATION IN THE QUESTION SHOULD BE INSERTED HERE

F = input('Enter the coefficients [a, b, c]: ');

f = input('Enter the RHS func on f(x): ');

a = F(1); b = F(2); c = F(3);

AE = a * m^2 + b * m + c; % Auxiliary Equa on

m = solve(AE);

m1 = m(1); m2 = m(2);

D = b^2 - 4 * a * c;

if D > 0 % Real and dis nct roots

y1 = exp(m1 * x);

y2 = exp(m2 * x);

elseif D == 0 % Real and equal roots

y1 = exp(m1 * x);

y2 = x * exp(m1 * x);

else % Complex roots

alfa = real(m1);

beta = imag(m1);

y1 = exp(alfa * x) * cos(beta * x);

y2 = exp(alfa * x) * sin(beta * x);

end

yc = c1 * y1 + c2 * y2;

fx = f / a;
W = y1 * diff(y2, x) - y2 * diff(y1, x); % Wronskian

u = int(-y2 * fx / W, x);

v = int(y1 * fx / W, x);

yp = y1 * u + y2 * v;

y_gen = yc + yp;

check = input('If the problem has ini al condi ons then enter 1 else enter 2: ');

if check == 1

cn = input('Enter the ini al condi ons [x0, y(x0), Dy(x0)]: ');

dy_gen = diff(y_gen);

eq1 = subs(y_gen, x, cn(1)) - cn(2);

eq2 = subs(dy_gen, x, cn(1)) - cn(3);

[c1, c2] = solve(eq1, eq2);

y = simplify(subs(y_gen));

disp('The complete solu on is:');

disp(y);

ezplot(y, [cn(1), cn(1) + 2]);

else

y = simplify(y_gen);

disp('The General Solu on is:');

disp(y);

end
LAPLACE METHOD== 4
clear all

clc

syms t s y(t) Y

dy(t) = diff(y(t));

d2y(t) = diff(y(t), 2);

% THE EQUATION IN THE QUESTION SHOULD BE INSERTED HERE

F = input("Input the coefficients [a, b, c]: ");

a = F(1); b = F(2); c = F(3);

nh = input("Enter the non-homogeneous part f(x): ");

eqn = a * d2y(t) + b * dy(t) + c * y(t) - nh;

LTY = laplace(eqn, t, s);

IC = input("Enter the ini al condi ons in the form [y0, Dy(0)]: ");

y0 = IC(1); dy0 = IC(2);

LTY = subs(LTY, {laplace(y(t), t, s), y(0), subs(diff(y(t), t), t, 0)}, {Y, y0, dy0});

eq = collect(LTY, Y);

Y = simplify(solve(eq, Y));

yt = simplify(ilaplace(Y, s, t));

disp("The solu on of the differen al equa on y(t) = ")

disp(yt)

fplot(yt, [y0, y0 + 2]);


MATRIX METHOD ==5
syms t C1 C2

A=input('Enter A: ');

[P D]=eig(A);

Sol1 = dsolve(['D2y = ',num2str(D(1)),'*y']);

Sol2 = dsolve(['D2y = ',num2str(D(4)),'*y']);

X = P*[Sol1;Sol2];

ch =input("Does the sum has ini al condi ons? (Y/N): ","s");

if upper(ch) == 'Y'

Cond=input('Enter the ini al condi ons [t0, x10,x20]: ');

t0=Cond(1);x10=Cond(2);x20=Cond(3);

eq1=subs(X(1)-x10,t0);eq2=subs(X(2)-x20,t0); [C1, C2] =

solve(eq1,eq2);

X=subs(X);

else

t0=0;x10=0;x20=0;

eq1=subs(X(1)-x10,t0);eq2=subs(X(2)-x20,t0); X=subs(X);

end

disp("x1= "); disp(X(1));

disp("x2= "); disp(X(2));


Z TRANSFORM == 6
clc

clear all

syms y(n) z Y

assume(n >= 0 & in(n, 'integer'))

% THE EQUATION IN THE QUESTION SHOULD BE INSERTED HERE

a = input('The coefficient of y(n+2) = ');

b = input('The coefficient of y(n+1) = ');

c = input('The coefficient of y(n) = ');

nh = input('Enter the non-homogeneous part = ');

c1 = input('Enter the condi on y(0) = ');

c2 = input('Enter the condi on y(1) = ');

f = a * y(n + 2) + b * y(n + 1) + c * y(n) - nh;

fZT = ztrans(f, n, z);

fZT = subs(fZT, ztrans(y(n), n, z), Y);

fZT = subs(fZT, y(0), c1);

fZT = subs(fZT, y(1), c2);

yZT = solve(fZT, Y);

ysol = iztrans(yZT, n);

ysol = simplify(ysol);
EIGEN VALUE AND EIGEN VECTOR==7
clc;

clear all;

syms L

A = input('Enter a square matrix A: ');

s = length(A); % Order of the square matrix

% Characteris c equa on: det(A - L * I) = 0

eq = det(A - L * eye(s)) == 0;

% Solve for eigenvalues

eigenvalues = solve(eq, L);

disp('Eigenvalues are:');

disp(eigenvalues);

% Solve for eigenvectors

for i = 1 : length(eigenvalues)

eigenvectors = null(A - eigenvalues(i) * eye(s));

fprin ('Eigenvector corresponding to Eigenvalue %d:\n', i);

disp(eigenvectors);

end

You might also like