Question: Your task is to write a MATLAB script that will plot a set of data and fit a curve to the data. The script should:

Your task is to write a MATLAB script that will plot a set of data and fit a curve to the data. The script should:

Part 1

1. Have a section that allows the user to enter the name of a data file.

2. Be capable of reading data stored in either rows or columns.

3. Allow the user to select the color and type of plot symbol.

4. Once the data is entered, it should plot the x - y data.

5. Enter a pause in the program so the user can view the output of the plot, then fit the data with a linear line. Do not use the polyfit command to find the coefficients of the equation. Instead use linear algebra to solve the equations for the best fit shown in the text book.

6. Allow the user to select the color of the line, and plot the x y data and best fit line on the same graph.

7. Calculate the absolute error and relative error. Have the script display the maximum value of both errors and the x coordinate corresponding to each maximum error. (Be careful with dividing by zero. What do you do????)

8. Calculate the r-squared value.

9. Add the equation of the line and the r-squared value to the graph (for now enter the words r-squared, we will show you how to enter r2 later).

10. Have a section that will allow the user to enter the plot title, and x and y axis labels.

11. Allow the user to estimate the value of the best fit for any x coordinate.

12. Once the user is satisfied with the results, the script should allow the user to enter a new set of data without ending the program. Do not use a menu for this. Make sure the script allows for user input error (ie. Check for words versus characters and for incorrect characters)

13. Once you have completed this, enter the same data sets into Excel and fit a trend line to the data. Do you get the same equations and r-squared value? What software would you use it given this task to do for another class?

Part 2

14. Once the script is complete, modify it so the user has the option to fit a linear line or a polynomial. Use an if statement to select the choice.

15. For the polynomial fit use polyfit, the program should use at least 300 data points to plot the curve and the program should be capable of starting the curve at the smallest and largest x coordinate without user input.

16. Once the polynomial is fit to the data the program should allow the user to try different degree fits to the data. Make sure you check for the maximum degree.

17. Add the equation to the plot, along with titles and labels. Note, adding the equation to the plot requires you to build a string array.

Procedure: Given below is a table containing two sets of data. (Data set 1 would be the x column and the data set 1 column. Data set 2 would be the x column and data set 2 column.) Enter these data into two separate data files. Data set 1 should be entered as rows and named rowdata.dat. Data set 2 should be entered as columns and named colsdata.dat. Then prepare individual plots of these data with the best fit curves and save the plots as figure1.fig and figure2.fig. Be sure to give each plot a title, x & y-axis labels and a legend.

y values y values

data data

x set1 set2

0 1.4 -10.0

0.5 1.3 -5.0

1.0 1.1 3.0

1.5 0.9 6.0

2.0 0.7 10.4

3.0 0.4 20

4.0 0.27 40

5.0 0.18 55

6.0 0.1 95

7.0 0.0 100

8.0 -0.2 120

9.0 -0.3 160

10 -0.4 190

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!