Matlab assignment

profilerawan-76
Matlab5.zip

Matlab 5/CEE384 Matlab Assignment 5.pdf

CEE 384 Numerical Methods for Engineers Fall 2017 Arizona State University School of Sustainable Engineering and The Built Environment Dr. Lou

Page 1 of 2

MATLAB Assignment 5

Released: 11/5/2017 Due: 11/22/2017 at 08:00 AM

Please read “MATLAB Assignment Submission Guidelines” in Blackboard before submission. Not following the guidelines will result in loss of credit, even though you may have the correct answer(s).

Write code of your own and answer the questions in this assignment.

 Submit your code through Cody Coursework for task 1.  Submit a report through Blackboard.

Numerical integration methods are the basis for this assignment. [Hints] While loops, for loops, and if-else-end statements among other MATLAB commands may be needed to complete this assignment. When in trouble, consult the Help feature.

In an attempt to understand the mechanism of the depolarization process in a direct methanol fuel cell

(DMFC), an electro-kinetic model for mixed oxygen-methanol current on platinum was developed in the

laboratory at Florida A&M University (read more about DMFC in textbook Ch 07.00B). A simplified

model of the reaction developed suggests a functional relation in an integral form:

𝑇 = ∫ 6.73𝑥 + 6.725 × 10−8 + 7.26 × 10−4𝐶𝑚𝑒

3.62 × 10−12𝑥 + 3.908 × 10−8𝑥𝐶𝑚𝑒 𝑑𝑥

𝑥1

𝑥2

where

𝑇 = Time it takes to consume certain amount of oxygen concentration in the fuel cell, sec

𝑥 = Concentration of oxygen, moles/cm3

𝑥1 = Initial concentration of oxygen, moles/cm3

𝑥2 = Concentration of oxygen after T seconds, moles/cm3

𝐶𝑚𝑒 = Concentration of methanol, moles/cm3

0 ≤ 𝐶𝑚𝑒 ≤ 5 × 10−4 moles/cm3

0.2 × 10−6 ≤ 𝑥 ≤ 1.22 × 10−6 moles/cm3

Task 1

Use MATLAB to evaluate the time required for the initial oxygen concentration to be reduced by half

(𝑥2 = 𝑥1/2) in the fuel cell using various numerical integration methods.

Write a function to perform analytical and various numerical integration methods for the integration.

Your function should meet the following requirements:

 It should be named EKdmfc

 The function should have three input arguments: 1) Concentration of methanol, 𝐶𝑚𝑒 2) Initial concentration of oxygen, 𝑥1 3) The number of intervals, n

 The function should have a single output argument in the form of a row vector that contains five items (in this order):

CEE 384 Numerical Methods for Engineers Fall 2017 Arizona State University School of Sustainable Engineering and The Built Environment Dr. Lou

Page 2 of 2

1) The value of the analytical integral using the built-in ‘int’ command. 2) The value of the numerical integral using the built-in ‘integral’ command. 3) The value of the numerical integral using the built-in ‘quadgk’ command. 4) The value of the numerical integral using the built-in ’trapz’ command with the input

value of number intervals 𝒏. 5) The value of the numerical integration using Simpson’s 1/3 rule. Write your own code to

perform multiple-segment Simpson’s 1/3 rule.

 Note that for multiple-segment Simpson’s 1/3 rule, 𝒏 must be an even natural number (read more about this in Ch 07.03). Your code should check the input value of 𝑛, and return ‘inf’ for this part if 𝑛 does not meet this requirement. (Hint: an even number is divisible by 2 while an odd number is not. In other words, the remainder after division by 2 for an even number is 0, but is 1 for an odd number. There are at least two MATLAB built-in functions that you can use to obtain the remainder after division.) Demonstrate this portion of your code in your report.

 Display the input and output in the command window. The display should be professionally formatted and the precision of the display controlled.

Your solution will be tested three times with randomly generated input arguments.

Task 2

Assume 𝐶𝑚𝑒 = 3 × 10−4, and 𝑥1 = 1.00 × 10−6. Analyze the results from your function and compare

the resulting integral values from different methods. Employ the concepts of errors for different

numerical integration methods you have learned in this course and discuss the accuracy of these

results. Explain why certain methods are more accurate than others. Use your code, explore how the

error from the trapezoidal method and the Simpson’s 1/3 method change as you vary the number of

intervals.

Task 3

Plot time required vs. initial oxygen concentration for a range of 𝑥1 values (0.2 × 10−6 ≤ 𝑥1 ≤ 1.22 ×

10−6) for 𝐶𝑚𝑒= 3.5 × 10−4 moles/cm3. Initial oxygen concentration (𝑥1) should be on the x-axis and

time is on the y-axis. From your plot, what is the relation (Linear, Quadratic, Cubic, Exponential etc.)

between time required vs. initial oxygen concentration? Try to prove your opinion.

__MACOSX/Matlab 5/._CEE384 Matlab Assignment 5.pdf

Matlab 5/Matlab Assignment Submission Guidelines(1).pdf

CEE 384 Numerical Methods for Engineers Fall 2017 Arizona State University School of Sustainable Engineering and The Built Environment Dr. Lou

Page 1 of 1

MATLAB Assignment Submission Guideline When preparing your assignments/report, your objective is to communicate information to the reader (in this case, your instructor) in a Clear, Concise, Complete, Careful, and Courteous manner (5 C’s of good writing). If your work does not possess the “5C” qualities and/or does not adhere to the guidelines specified below, you will lose credit even though you may have the correct answer(s). In cases of serious offence, you may not receive any credit for the work. This policy will be strictly enforced beginning with the very first assignment.

1. MATLAB assignments must be submitted by their corresponding deadlines. You must submit 1). A report in the form of a PDF file through Blackboard; AND 2). Your code through Cody Coursework.

Late submission will not be graded or receive any credit. No paper copy submissions, scanned hand-written work, or .m files will be accepted, unless otherwise specified by the instructor.

2. For the report submission:

• You must prepare a cover page for the report. It should include 1). Course information (such as course number, section number, semester, year, etc.) 2). Assignment information (such as assignment number, topic, due date etc.) 3). Preparer information (such as name, major, ASUID, etc.)

• The report must be very neat and well-organized. Maintain a minimum margin of 1” for all four sides.

• The report should include the following elements wherever applicable. 1) Introduction and problem statement; 2) Methodology (background theory, logic in your code, excerpts of your code, etc.); 3) Results; 4) Discussion; and 5) Conclusion

• The report must not exceed 5 pages. 1). Cover page does not count toward this page limit. 2). You may choose to include excerpts of your code in appendices, which do not count

toward this page limit. 3). You may also choose to present figures, tables, etc. at the end of your report, which do

not count toward this page limit. Figures, tables, etc. must be properly labeled. • Excerpts of your code must be screen snips from your local MATLAB environment. To capture

a specific area of your screen, use the Snipping Tool for Windows systems; and press COMMAND+SHIFT+4 at the same time for Mac. Do not copy the entire code in your report.

3. For the code submission:

• Provide adequate comments as appropriate. For example, you must provide the definition of all variables used along with units. Your will be graded on the readability of your MATLAB code among other grading rubrics.

If you have any questions regarding submission of your homework, play it safe: CHECK WITH THE INSTRUCTOR. The primary aim in specifying these guidelines is to ensure that your work reflects the professional nature of the career path you have chosen. Finally, guidelines may be modified, deleted, or added at the discretion of the instructor.

__MACOSX/Matlab 5/._Matlab Assignment Submission Guidelines(1).pdf

Matlab 5/MATLAB Tutorial 5 Numerical Integration.pdf

MATLAB Tutorial 5: Integration

CEE 384: NUMERICAL METHODS FOR ENGINEERS

Outline

Analytical Integration

Numerical Integration

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 2

Analytical Integration

Go back to the Outline slide

Built-in int function The built-in int function

◦ performs analytical integration

◦ is suitable for both definite and indefinite integrals

◦ is symbolic

◦ is used when the integrand equation has an analytical expression

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 4

Built-in int function For definite integration:

◦ int(func, var, lower, upper)

func is the function to integrate i.e. the integrand

◦ func must be a symbolic expression

var is the variable to integrate with respect to

◦ Think of x for dx, y for dy, z for dz

lower is the lower bound of the integral

upper is the upper bound of the integral

For indefinite integration:

◦ int(func, var)

func and var are just like those mentioned above

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 5

Try It Out!

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 6

Built-in function int

Note that x is defined as a symbolic variable here, thus y is also symbolic. This is necessary because the first input argument to the int function must be symbolic.

Numerical Integration

Go back to the Outline slide

Numerical Integration Numerical integration-

◦ Integration that approximates the area under the curve

trapz(x,y)

◦ takes x and y vectors as inputs

◦ uses trapezoidal rule to approximate the integral based on the size of the y vector

◦ can be used for matrices

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 8

trapz example for y = x^2 vector data

Try It Out!

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 9

Built-in function trapz

Numerical Integration Other available built-in functions

◦ integral(func, xmin, xmax)

◦ quadgk(func, xmin, xmax)

In the above

◦ func is the function handle of the integrand

Associated with anonymous functions

◦ xmin is the lower bound

◦ xmax is the upper bound

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 10

Also see MATLAB Tutorial 2 for details.

q=integral(func,xmin,xmax)

For func, xmin, and xmax, see slide 10

The output q is the estimated integral

How it works

◦ Uses global adaptive Simpson’s rule and default error tolerances Applies Simpson’s 1/3

 Iteratively doubles the number of intervals and tracks approximation error

 Stops and returns integral estimate when approximation error is less than some default error tolerance value

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 11

Example use: Example output:

Try It Out!

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 12

Built-in function integral

quadgk(func,xmin,xmax)

For func, xmin, and xmax (see slide 10)

quadgk has two output options ◦ q=quadgk(func,xmin,xmax)returns the integral estimate q

◦ [q, errbnd]=quadgk(func,xmin,xmax)returns the integral estimate q and the estimated upper bound of the absolute true error errbnd

How it works ◦ Uses a variant of Gauss quadrature (called Gauss-Kronrod quadrature)

◦ Iteratively doubles the number of intervals and tracks approximation error

◦ Stops and returns integral estimate when approximation error is less than some default error tolerance value

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 13

Example use: Example output:

Try It Out!

NUMERICAL METHODS FOR ENGINEERS ARIZONA STATE UNIVERSITY DR. YINGYAN LOU 14

Built-in function quadgk

__MACOSX/Matlab 5/._MATLAB Tutorial 5 Numerical Integration.pdf

Matlab 5/Probelm description .png

__MACOSX/Matlab 5/._Probelm description .png

Matlab 5/Task 1 Analytical and Numerical integration methods.m

function = EKdmfc(Cme, x1, n) % integrand: f = (a*x+b+c*Cme)/(d*x+e*Cme*x) a = 6.73; b = 6.725*10^-8; c = 7.26*10^-4; d = 3.62*10^-12; e = 3.908*10^-8; end

__MACOSX/Matlab 5/._Task 1 Analytical and Numerical integration methods.m