F23_Lab08_CurveFitting

pdf

School

University of Toronto *

*We aren’t endorsed by this school

Course

ECE311

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

6

Uploaded by MegaMusic12706

Report
CPS118 > Lab 05 > Plotting in MATLAB 1 CPS118 > Lab 08 MATLAB's Curve Fitting and Simple Python [Fall 2023] I. Overview and Objectives MATLAB has an array of tools for modeling data and one can use the models obtained to estimate values for data points other than those observed and recorded. Curve ±tting in MATLAB is done using polynomials and other types of functions. Polynomials can be easily represented and operated on in MATLAB. The estimated models can also be easily obtained by writing programs, or using the interactive data analysis tools available through the Figure window user interface. Python is another general purpose interpreted programming language that is very popular in practice. It has a simple syntax and grammar, and it can be utilized in writing simple and complex code. In this course, we had a quick introduction to this programming language. The main goal of this lab is to allow you the opportunity to explore these tools and functions. The learning objective of this lab can be summarized as follows: 1. Representing and operating on polynomials, 2. Curve ±tting of data using di²erent functions, 3. Estimation of data using polynomial-based interpolation. 4. Writing simple scripts using the Python language. Reading and related topics: Lecture slides (Lessons #9 and #10) and chapter 8 of
CPS118 > Lab 05 > Plotting in MATLAB 2 Amos Gilat’s book. II. Lab Tasks For this lab, do any 4 of the following 5 problems . You can choose to do all 5 problems, in that case you will get the mark of the highest 4. Try to do problem 5 even if you do not submit it so that you can get the "feel" of the Python language. Use Matlab to solve the ±rst four problems, and Python to solve the ±fth problem. Save the code you wrote to solve them, together with the result of it in a report. Make sure you include enough comments in your code. Save your report in PDF format. You can use the MLX format if you do only problems 1 to 4. Problem 1: Write a user-de±ned function that adds or subtracts two polynomials of any order. Name the function p = polyadd(p1, p2, operation) . The ±rst two input arguments p1 and p2 are the vectors of the coe³cients of the two polynomials. (If the two polynomials are not of the same order, the function adds the necessary zero elements to the shorter vector.) The third input argument operation is a string that can be either ‘add’ or ‘sub’, for adding or subtracting the polynomials, respectively, and the function's result is the resulting polynomial. Try your code on several di²erent inputs, and show the results. Problem 2: Consider the parabola: x = 2(y − 2) 2 + 3, and the point P(3, 4).
CPS118 > Lab 05 > Plotting in MATLAB 3 (a) Write a polynomial expression for the distance d from point P to an arbitrary point Q on the parabola. (b) Make a plot of d versus y for 0 ≤ y ≤ 4. (c) Determine the coordinates of Q if d = 3 (there are two points). (d) Determine the coordinates of Q that correspond to the smallest d, and calculate the corresponding value of d. (e) Make a plot that shows the parabola, point P, the two points from part (c), and the point from part (d). Problem 3: Growth data of a sun´ower plant is given in the following table: (a) Curve-±t the data with a third-order polynomial. Use the polynomial to estimate the height on day 40. (b) Fit the data with linear and spline interpolations and use each interpolation to estimate the height on day 40.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CPS118 > Lab 05 > Plotting in MATLAB 4 In each part make a plot of the data points (circle markers) and the ±tted curve or the interpolated curves. Note that part (b) has two interpolation curves. Problem 4: When rubber is stretched, its elongation is initially proportional to the applied force, but as it reaches about twice its original length, the force required to stretch the rubber increases rapidly. The force, as a function of elongation, required stretching a rubber specimen that was initially 3 in. long is displayed in the following table: (a) Curve-±t the data with a fourth-order polynomial. Make a plot of the data points and the polynomial. Use the polynomial to estimate the force when the rubber specimen was 11.5 in. long. (b) Fit the data with spline interpolation (use MATLAB’s built-in function interp1). Make a plot that shows the data points and a curve made by interpolation. Use interpolation to estimate the force when the rubber specimen was 11.5 in. long. Problem 5: In most universities, course grades are reported using a letter grade, i.e. A, B, · · · , whereas the overall performance is reported using Grade Point Average (GPA). First, a number (typically in the range from 0 to 4) is assigned to each letter grade. The GPA is calculated by summing the weighted grade points (credit hours * numerical value of the grades) then dividing by the total amount of credit hours. So, for example suppose, consider the simpli±ed grade scale that assigns letter grades of ’A’, ’B’, ’C’, ’D’, and ’F’, to grade points ’4’, ’3’, ’2’, ’1’, and ’0’, respectively. If your grades for Biology (3 credits), Math (4 credits), Computer Science (3 credits), and Physics (2 credits) are ‘BBAD ’, then you GPA will be
CPS118 > Lab 05 > Plotting in MATLAB 5 calculated as (rounded to a decimal digit): (a) Write a Python user-de±ned function that calculates the GPA on a scale of 0 to 4, as above. That is A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take two input vectors, ’Grades’ containing the letter grades and ’Hours’ containing the credit hours for each course in ’Grades’. It should output the GPA. Hint: You can consider using the following Python commands: sum, len, and if-elif-else. (b) Suppose your grades for a term are as stated in the example above. That is they are [’B’, ’B’, ’A’ ,’D’] for Biology (3 credits), Math (4 credits), Computer Science (3 credits), and Physics (2 credits). Your program should create two lists, one for the grades and one for their corresponding hours. It should then use your user-de±ned function from part (a) to calculate and display the resulting GPA. Important: You have to do this problem in Python. You will not receive any credit if you do this problem in Matlab. In your report, include your Python .py script ±le that should include your function ±le (with su³cient comments). III. Submission Guidelines This lab is to be done individually. You need to submit a lab report with the solutions to the four or ±ve problems, and the report submitted should represent your work. Your lab report must be in PDF format. Submit your lab report on D2L/Brightspace before December 4, 2023 at 11:59 PM . Late submissions will not be accepted.
CPS118 > Lab 05 > Plotting in MATLAB 6 Hav± fu²!
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help