% IAP2007 Introduction to MATLAB: Interface and Basics % Instructor: Violeta Ivanova, violeta@mit.edu % EXERCISE 2 Curve Fitting % MATLAB vectors, polynomials, matrix operations, curve fitting, plotting. % The coordinates (x1,y1), (x2,y2), (x3,y3) and (x4,y4) of four points are % given: (1, 54), (5, 82), (8, 40), and (10, 10). % Find a cubic equation y(x) that passes through the four points i.e.: % y = c1*x^3 + c2*x^2 + c3*x + c4 % Fit also a straight line and a quadratic curve to the points. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % A. VECTORS % A1. Define column vectors X and Y from the points' coordinates such that % X = (x1, x2, x3, x4) and Y = (y1, y2, y3, y4) % Create a column vector X by transposing a row vector: % Create a column vector Y by entering its elements row by row: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % B. CURVE FITTING % Compute straight line, quadratic, and cubic fits with function POLYFIT. % B1. Straight line fit: % B2 Quadratic fit: % B2 Cubic fit: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % C. POLYNOMIALS % C1. Compute a cubic curve, a quadratic curve, and a straight line over an % x interval from 0 to 10 at a step of 0.1. Hint: use function POLYVAL. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % D. PLOTTING % D1. Plot the cubic curve, quadratic curve, straight line, and four points % on the same figure. Note: use different colors and/or markers. % Hint: use functions PLOT, HOLD ON, and HOLD OFF. % D2. Add a title, X and Y labels, and a legend to the figure. % Hint: Use functions TITLE, XLABEL, YLABEL, and LEGEND. % D3. Customize the figure using the PROPERTY EDITOR. % D4. Save the figure in a JPEG file.