Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

MATLAB Grader Que.2

This document is a MATLAB Grader assignment that involves solving a 1D diffusion problem using finite differences. The problem describes setting up a grid, deriving the finite difference equations, and solving the linear system to find the numerical solution. The student's script implements the finite difference method and calculates the error between the numerical and exact solutions at a point.

Uploaded by

Saurabh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

MATLAB Grader Que.2

This document is a MATLAB Grader assignment that involves solving a 1D diffusion problem using finite differences. The problem describes setting up a grid, deriving the finite difference equations, and solving the linear system to find the numerical solution. The student's script implements the finite difference method and calculates the error between the numerical and exact solutions at a point.

Uploaded by

Saurabh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

8/13/23, 1:00 PM MATLAB Grader

MATLAB Grader (/)


ME623: Finite Element Method in Engineering Mechanics (/courses/46893-me623-finite-element-method-
FILTER CONTENTS
in-engineering-mechanics) Quiz 1: Some Basic Concepts (/courses/46893-me623-finite-element-method-
in-engineering-mechanics/assignments/132927-quiz-1-some-basic-concepts)

1-d finite difference (/courses/46893-me623-finite-


element-method-in-engineering-
mechanics/assignments/132927-quiz-1-some-
basic-concepts/problems/467687-1-d-finite-
difference)
0 solutions submitted (max: 6)

1-d finite difference


Problem title
A diffusion problem is defined by the ordinary differential equation
d 2C
− C = 0,
d x2
where C (x) is the concentration of a species at a position x, and x ∈ [0, 1]. The boundary c
at x = 0, C (0) = 1 and
dC
at x = 1, = 0.
dx
1
Divide the domain $x \in [0,1]$ into N equal divisions of size h = , i.e N + 1 grid points. L
N
points be denoted by
C0 , C1 , … C N .
The boundary condition at x = 0 immediately gives the equation
C0 = 1.
To impose the boundary conditions, especially the condition at x = 1, we will do a small (but
not do for the heat conduction problem worked out in the class. We will extend the domain to
grid point at x = x N +1 = 1 + h.
We now have to determine the values of C (x) at the N + 2 grid points, i.e. C0, C1, … C N −1, C N , C
In the central difference scheme, we can approximate
dC C N +1 − C N −1
∣ = , in order to impose the boundary condition at x = 1, i.e. x N .
dx ∣N 2h
https://grader.mathworks.com/courses/46893-me623-finite-element-method-in-engineering-mechanics/problems/467687-1-d-finite-difference/solutions… 1/4
8/13/23, 1:00 PM MATLAB Grader

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.

Output Run Script


Cmatrix = 1.0000 0 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000
-201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000

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

100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0

MATLAB Grader (/)


0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0
100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 -5.0000 0
Cmatrix = 1.0000 0 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000
100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0
0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0
FILTER CONTENTS
100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000
-201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 100.0000 -201.0000 100.0000 0 0 0 0 0 0 0 0 0 0 -5.0000 5.0000 mid_erro
= 0.0104 C_exact(0.5): 0.730763 C_numerical(0.5): 0.720394 Absolute error at x = 0.5: 0.010369
Output of your solution

Run Pretest Submit (Attempt 1 of 6)


Assessment:
Match the value of Cexact, the exact solution

Match Cmatrix
+
Match Cnumerical (Pretest)

Assessment Results for your solution

Trust Center (https://www.mathworks.com/company/aboutus/policies_statements/patents.html?


s_tid=gf_pat)
Trademarks (https://www.mathworks.com/company/aboutus/policies_statements/trademarks.html?
s_tid=gf_trd)
Privacy Policy (https://www.mathworks.com/company/aboutus/policies_statements.html?s_tid=gf_priv)
Preventing Piracy (https://www.mathworks.com/company/aboutus/policies_statements/piracy.html?
s_tid=gf_pir)
Application Status (https://status.mathworks.com/?s_tid=gf_application)

© 1994-2023 The MathWorks, Inc.

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

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

You might also like