0% found this document useful (0 votes)
34 views12 pages

Engineering Analysis and Design

The program solves the heat equation ut(x,t) = c^2uxx(x,t) subject to boundary and initial conditions. It uses the Sturm-Liouville theory to find the eigenvalues and eigenfunctions. The solution is obtained by separating variables as a sum of sine functions multiplied by exponential terms, satisfying the initial and boundary conditions.
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)
34 views12 pages

Engineering Analysis and Design

The program solves the heat equation ut(x,t) = c^2uxx(x,t) subject to boundary and initial conditions. It uses the Sturm-Liouville theory to find the eigenvalues and eigenfunctions. The solution is obtained by separating variables as a sum of sine functions multiplied by exponential terms, satisfying the initial and boundary conditions.
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/ 12

PROGRAM-6 :

Write a program to solve (aD^2 + bDD’ + cD’^2 )z = f(x,y)


Theory:
A linear partial differential equation with constant coefficients is known as non homogeneous linear partial
differential equation with constant coefficients if the orders of all the partial derivatives involved in the
equation are not equal. Here we are dealing with only homogeneous PDE with constant coefficients.

Code :
%Function Name
function [] = HomoPDE()
syms D d x y z m f1 f2 f3
%D is wrt x and d is wrt y
%We take f1, f2 and f3 inplace of phi1, phi2 and phi3
%F is LHS and f is RHS
F = (D*D*D - 6*D*D*d + 11*D*d*d - 6*d*d*d)*z;
f = exp(5*x + 6*y);
%Substitute D with m and d with 1
eqn1 = subs(F, D, m);
eqn2 = subs(eqn1, d, 1)

%Values of m as a 3*1 matrix


mval = solve(eqn2, m)
%Find CF
if mval(1)~=mval(2)~=mval(3)
CF = f1*(y+mval(1)*x) + f2*(y+mval(2)*x) + f3*(y+mval(3)*x)
elseif mval(1) == mval(2) ~= mval(3)
CF = f1*(y+mval(1)*x) + x*f2*(y+mval(2)*x) + f3*(y+mval(3)*x)
elseif mval(1) ~= mval(2) == mval(3)
CF = f1*(y+mval(1)*x) + x*f2*(y+mval(2)*x) + f3*(y+mval(3)*x)
elseif mval(1) == mval(3) ~= mval(2)
CF = x*f1*(y+mval(1)*x) + f2*(y+mval(2)*x) + f3*(y+mval(3)*x)
else
CF = f1*(y+mval(1)*x) + x*f2*(y+mval(2)*x) + x*x*f3*(y+mval(3)*x)
end
%Find equation of variables D and d
F1 = F/z;
F2 = f;
%Find values of constants of x and y
fpi = log(f);
valx = diff(fpi,x)
valy = diff(fpi,y)
2K22/MC/60
%Substitute values of x and y in F1
while true
eqn3 = subs(F1,D,valx);
eqn4 = subs(eqn3,d,valy);
if eqn4~=0
break
else
F1 = diff(F1,D);
F2 = F2*x;
end
end
%Find PI
PI = F2/eqn4
%Final answer
final_ans = CF+PI

Results:
>> HomoPDE
eqn2 =
z*(m^3 - 6*m^2 + 11*m - 6)

mval =

1
2
3

CF =

f1*(x + y) + f2*(2*x + y) + f3*(3*x + y)

valx =

valy =

2K22/MC/60
PI =

-exp(5*x + 6*y)/91

final_ans =

f1*(x + y) - exp(5*x + 6*y)/91 + f2*(2*x + y) + f3*(3*x + y)

>>

Code I:
%Function Name
function [] = HomoPDE_cos()
syms D d f1 f2 f3 y x z m c
%D is wrt x and d is wrt y
%We take f1 and f2 inplace of phi1 and phi2
%F is LHS and f is RHS
F = (D*D - 3*D*d + 2*d*d)*z;
f = 2*cos(x+3*y);

%Substitute D with m and d with 1


eqn = subs(subs(F, D, m), d, 1);
%Values of m as a 3*1 or 2*1 matrix
mval = solve(eqn, m)
%Find CF
if mval(1)~=mval(2)
CF = f1*(y+mval(1)*x) + f2*(y+mval(2)*x)
else
CF = f1*(y+mval(1)*x) + x*f2*(y+mval(2)*x)
end
%Find coeff of x and y
fn = f/2
eqnx = acos(eval(subs(subs(fn,y,0),x,1)))
eqny = acos(eval(subs(subs(fn,x,0),y,1)))
%Find PI
PIF = F/z;
PI_deno = subs(subs(subs(PIF,D*D,(-1)*eqnx*eqnx) ,D*d,(-1)*eqnx*eqny) ,d*d,(-1)*eqny*eqny)
PI = f/PI_deno
%Final answer
final_ans = CF + PI
2K22/MC/60
Results:
>> HomoPDE_cos

mval =

1
2

CF =

f1*(x + y) + f2*(2*x + y)

fn =

cos(x + 3*y)

eqnx =

eqny =

3.0000

PI_deno =

-10

PI =

-cos(x + 3*y)/5

final_ans =

f1*(x + y) - cos(x + 3*y)/5 + f2*(2*x + y)

>>

Code II:
%Function Name
function [] = HomoPDE_sin()
syms D d f1 f2 f3 y x z m c

2K22/MC/60
%D is wrt x and d is wrt y
%We take f1 and f2 inplace of phi1 and phi2
%F is LHS and f is RHS
F = (D*D - 5*D*d + 4*d*d)*z;
f = sin(2*x+3*y);

%Substitute D with m and d with 1


eqn = subs(subs(F, D, m), d, 1);
%Values of m as a 3*1 or 2*1 matrix
mval = solve(eqn, m)
%Find CF
if mval(1)~=mval(2)
CF = f1*(y+mval(1)*x) + f2*(y+mval(2)*x)
else
CF = f1*(y+mval(1)*x) + x*f2*(y+mval(2)*x)
end
%Find coeff of x and y
%sin(x) = sin(pi - x)
eqnx = pi - asin(simplify(subs(subs(f,y,0),x,1)))
eqny = pi - asin(simplify(subs(subs(f,x,0),y,1)))
%Find PI
PIF = F/z;
PI_deno = subs(subs(subs(PIF,D*D,(-1)*eqnx*eqnx) ,D*d,(-1)*eqnx*eqny) ,d*d,(-1)*eqny*eqny)
PI = f/PI_deno
%Final answer
final_ans = CF + PI

Results:
>> HomoPDE_sin
mval =
1
4

CF =

f1*(x + y) + f2*(4*x + y)

eqnx =

2K22/MC/60
eqny =
3

PI_deno =

-10

PI =

-sin(2*x + 3*y)/10

final_ans =

f1*(x + y) - sin(2*x + 3*y)/10 + f2*(4*x + y)

>>

Code :
%Function Name
function [] = HomoPDEgen_sol()
%t is any random variable
syms D d f1(t) f2(t) f3(t) y x z m c
%D is wrt x and d is wrt y
%We take f1, f2 and f3 inplace of phi_1, phi_2 and phi_3
%F is LHS and f is RHS
F = (D*D*D + 2*D*D*d - D*d*d- 2*d*d*d)*z;
f = (y+2)*exp(x);

%Making the constant of D to 1


Fconst = subs(subs(subs(F,D,1),z,1),d,0);
F = F/Fconst;
f = f/Fconst;
%Substitute D with m and d with 1
eqn = subs(subs(F, D, m), d, 1);
%Values of m as a 3*1 or 2*1 matrix
mval = solve(eqn, m)
n = numel(mval);
%Find CF
if n==2
2K22/MC/60
if mval(1)~=mval(2)
CF = f1(y+mval(1)*x) + f2(y+mval(2)*x)
else
CF = f1(y+mval(1)*x) + x*f2(y+mval(2)*x)
end
elseif n==3
if mval(1)~=mval(2)~=mval(3)
CF = f1(y+mval(1)*x) + f2(y+mval(2)*x) + f3(y+mval(3)*x)
elseif mval(1) == mval(2) ~= mval(3)
CF = f1(y+mval(1)*x) + x*f2(y+mval(2)*x) + f3(y+mval(3)*x)
elseif mval(1) ~= mval(2) == mval(3)
CF = f1(y+mval(1)*x) + x*f2(y+mval(2)*x) + f3(y+mval(3)*x)
elseif mval(1) == mval(3) ~= mval(2)
CF = x*f1(y+mval(1)*x) + f2(y+mval(2)*x) + f3(y+mval(3)*x)
else
CF = f1(y+mval(1)*x) + x*f2(y+mval(2)*x) + x*x*f3(y+mval(3)*x)
end
end
%Find PI
PIn = f;
while n>0
PIn = subs(PIn,y,c-mval(n)*x);
PIn = int(PIn,x);
Value_m = mval(n);
PIn = subs(PIn,c,y+mval(n)*x);
n = n-1;
end
%Final Particular Integral
PI = simplify(PIn)
%Final answer
final_ans = CF + PI

Results:
>> HomoPDEgen_sol
mval =
-2
-1
1

CF =

f1(y - 2*x) + f2(y - x) + f3(x + y)

2K22/MC/60
PI =

y*exp(x)

final_ans =

f1(y - 2*x) + f2(y - x) + f3(x + y) + y*exp(x)

2K22/MC/60
PROGRAM-7
Write a program to solve heat equation u_t(x,t) = c^2*u_xx(x,t) subject
to boundary conditions and the initial conditions.
Theory:
Let u(x,t) denote the temperature at point x at time t. The equation governing this setup is the so-called one-
dimensional heat equation: u_t(x,t) = c^2*u_xx(x,t), where k>0 is a constant (the thermal conductivity of the
material).
Boundary Conditions:
u(0,t) = u(a,t) = 0 for all t
Initial Condition:
u(x,0) = f(x)
0<x<a , t>0

Code :
sturm_liouville.m
function [e_value, e_function] = sturm_liouville(L)
syms y(x)
syms lambda n
%Equation is, y" + (lambda)y = 0
L = 1;
%L = pi/4;

%sprintf stores the string in buffer


sprintf('Solving for various conditions...')
sprintf('When lambda > 0')
assume(lambda > 0);
%dsolve computes symbolic solutions to ordinary differential equations
%sol = diff(X,n) applies diff recursively n times, resulting in the nth difference
solution = dsolve(diff(y, 2) + lambda * y == 0);
e_function = solution
diff_sol = diff(solution, x);
%subs(s) returns a copy of s , replacing symbolic variables in s , with their values obtained from the calling
function
vals = solve(subs(diff_sol, x,0) == 0, subs(diff_sol, x,L) == 0);
non_zero = vals;
sprintf('Non-zero values in the solution')
vals;
%Eigen Value for this solution
e_value = [(n * pi) / L] .^ 2;
2K22/MC/60
sprintf('When lambda = 0')
try
solution = dsolve(subs(diff(y, 2) + lambda * y == 0), lambda, 0);
catch
sprintf('No non-trivial solution')
end
sprintf('When lambda < 0')
assume(lambda < 0);
solution = dsolve(diff(y, 2) + lambda * y == 0);
diff_sol = diff(solution, x);
%No Explicit non trivial solution exists
vals = solve(subs(diff_sol, 0) == 0, subs(diff_sol, L) == 0);

Heat_eqn.m
syms x T B Bn C C1 C2 Ux lambda
%INCLUDE FUNCTION
%e_value is the p term which is equal to root(lambda)
[e_value, e_function] = sturm_liouville()
e_value = sqrt(e_value);
%SOLUTION
sol1 = subs(e_function,lambda,e_value)
%we can also use dsolve for this
sol = int(1/T,T)==int(-e_value*e_value*k,t);
sol2 = C*solve(sol,T)
sol1 = subs(sol1,C1,0)
Ux = sol1*sol2;
Ux = subs(Ux,-C*C2,Bn)
%Bn is half range fourier sine series

Results:
>> Heat_eqn
ans =
'Solving for various conditions...'

ans =

'When lambda > 0'

e_function =
2K22/MC/60
C1*cos(lambda^(1/2)*x) - C2*sin(lambda^(1/2)*x)

ans =

'Non-zero values in the solution'

ans =

'When lambda = 0'

ans =

'No non-trivial solution'

ans =

'When lambda < 0'

Warning: Unable to find explicit solution. For options, see help.


> In sym/solve (line 317)
In sturm_liouville (line 44)
In Heat_eqn (line 5)
e_value =

n^2*pi^2

e_function =

C1*cos(lambda^(1/2)*x) - C2*sin(lambda^(1/2)*x)

sol1 =

C1*cos(x*(pi*(n^2)^(1/2))^(1/2)) - C2*sin(x*(pi*(n^2)^(1/2))^(1/2))

Warning: Solutions are only valid under certain conditions. To include parameters
and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
> In sym/solve>warnIfParams (line 478)
In sym/solve (line 357)
In Heat_eqn (line 13)

sol2 =

2K22/MC/60
C*exp(-k*n^2*t*pi^2)

sol1 =

-C2*sin(x*(pi*(n^2)^(1/2))^(1/2))

Ux =

Bn*exp(-k*n^2*t*pi^2)*sin(x*(pi*(n^2)^(1/2))^(1/2))

>>

2K22/MC/60

You might also like