Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a numerical code in Matlab to solve the following differential equation us

ID: 3567484 • Letter: W

Question

Write a numerical code in Matlab to solve the following differential equation using the finite difference method: with boundary conditions: In the above. M = 2 + c where c is the last A Equation (1) admits a simple analytical solution of the form. Determine C1, C2, n1, and n2 B Apply the finite difference method to solve the above equation. In order to use the finite difference method, you need first discretize the differential equation. To do so. (1) First divide the domain into n segments. Here (ii) Approximate the derivatives using the central difference method that I showed you in class to discretize Eq. (1). In the end, you shall formulate linear equations Ax =b. looks like C. Consider that the A matrix is a special tridiagonal matrix. One can use the Thomas Algorithm to solve it with high efficiency. Please write the Thomas algorithm in Matlab and solve Ax =b D. Solve the above linear equations with different step size. Initially start with h =1/6. Continue to solve the equation with various h including h=1/6, 1/24. 1/48, 1/96. 1/384. Plot T as a function of x for different step sizes and plot the analytical solution as a function of x as well E. Plot the error as a function of the step size and fit the data into a polynomial function. What can you conclude (Him: the error can be defined as and T are, respectively, the approximate solution and the exact solution. Use Simpson?s rule to compute the integration.) F. Use the numerical solution to calculate the derivative dT(2)/dx and plot dT(2)/dx as a function of h (flint: Fit the data into a second-order Lagrange interpolating polynomial and then compute the derivative).

Explanation / Answer

% finite difference method.
global nt delx2

% set up the parameters
nt = 17
delx = 1/(nt-1)
delx2 = delx*delx
x(1) = 0.;
for i=2:nt-1
c0(i-1) = 0.;
x(i) = x(i-1)+delx;
end
x(nt) = x(nt-1)+delx

tbreak = [0 .001 .01 .1 1];
for kkk=1:4
tspan = [tbreak(kkk) tbreak(kkk+1)];
[t cc] = ode45('fd_rhs',tspan,c0);
nn=size(t);
c(kkk,:) = cc(nn(1),:);
c0(:) = c(kkk,:);
end

% add in the first and last point
for kkk=1:4
d(kkk,1) = 1;
for i=2:nt-1
d(kkk,i)=c(kkk,i-1);
end
d(kkk,nt) = 0;
end

plot(x,d(4,:),'g-*',x,d(3,:),'m-*',x,d(2,:),'b-*',x,d(1,:),'r-*')
xlabel('x')
ylabel('c')
legend('t = 1.0','t = 0.1','t = 0.01','t = 0.001')
title('Diffusion Problem Solved using Finite Difference Method')

******************************************************************

% fd_rhs.m
function ydot=fd_rhs(time,y)
global nt delx2

% add in the boundary conditions
c(1) = 1;
c(nt) = 0;

% transfer the y to the c
for i=1:nt-2
c(i+1) = y(i);
end

% calculate the right-hand side
for i=1:nt-2
dfd(i) = c(i) - 2*c(i+1) + c(i+2);
end
dfd = dfd/delx2;

% put result in the ydot
ydot = dfd';

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote