MATLAB Grader Que.2
MATLAB Grader Que.2
d 2C
Use the central difference formulas to approximate at a generic grid point x = xi and for
MATLAB Grader (/)
determine T
C = ⟨C C C … C C ⟩ 0 1 2 N N +1 .
d x2
First , find the exact solution to this problem by solving the governing ode with the given
exact solution be Cexact(x).
FILTER Then, complete the given Matlab code to solve the problem with the finite difference techniq
CONTENTS
absolute error∣Cnumerical(0.5) − Cexact(0.5)∣ for h = 0.1.
It will help if you chalk out the entire startegy for the solution and the structure of the
of paper first.
This problem has part markings.
Problem description
Solve the Problem.
Script
1 % quiz 1 problem 2 MATLAB Documentation
2 h=0.1; Opens in new tab
3 xactual=0:h:1; % grid points coordinates on [0,1] Reset (https://www.mathworks.com/help/)
4 N=length(xactual)-1; %number of divisions 1/h
5
% Exact solution
6
Cexact =(exp(2)/(exp(2)+1))*exp(-xactual) + (1/(exp(2)+1))*exp(xactual);
7
8 % FDM solution
9 Cmatrix= zeros(N+2,N+2); %initialise a N+2 X N+2 matrix
10 rhs = zeros(N+2,1); %initialise a N+2 X 1 vector
11 % modify the Cmatrix and rhs here
12 Cmatrix(1, 1) = 1;
13 rhs(1) = 1; % C(0) = 1
14 for i = 2:N+1
15
Cmatrix(i, i-1) = 1 / h^2;
16
Cmatrix(i, i) = -2 / h^2 - 1;
17
18 Cmatrix(i, i+1) = 1 / h^2;
19 end
20 % calculate the vector if C_0, ... C_{N+1} in Cnumerical
21 Cmatrix(N+2, N+1) = -0.5 / h;
22 Cmatrix(N+2, N+2) = 0.5 / h;
23 Cnumerical=Cmatrix \ rhs; % Solve the linear system
24 % find the values of Cexact and Cnumerical at x=0.5 and the absolute error
25
Cmid_exact=interp1(xactual,Cexact,0.5);
26
Cmid_numerical=interp1(xactual,Cnumerical(1:N+1),0.5);
27
28 mid_error = abs(Cmid_exact - Cmid_numerical)
29 fprintf('C_exact(0.5): %.6f\n', Cmid_exact);
f i tf('C i l(0 5) % 6f\ ' C id i l)
Script textarea. Use the MATLAB Editor to write your solution. To change focus out of the editor, press the Escape
Key. Then press Tab to move to the next field, Shift-Tab to move to the previous field, or Enter to return to the
MATLAB Editor.
https://grader.mathworks.com/courses/46893-me623-finite-element-method-in-engineering-mechanics/problems/467687-1-d-finite-difference/solutions… 2/4
8/13/23, 1:00 PM MATLAB Grader
Match Cmatrix
+
Match Cnumerical (Pretest)
https://grader.mathworks.com/courses/46893-me623-finite-element-method-in-engineering-mechanics/problems/467687-1-d-finite-difference/solutions… 3/4
8/13/23, 1:00 PM MATLAB Grader
FILTER CONTENTS
https://grader.mathworks.com/courses/46893-me623-finite-element-method-in-engineering-mechanics/problems/467687-1-d-finite-difference/solutions… 4/4