Plotting and Regression
PLOTTING, POLYNOMIALS, AND FIND
Key Functions and Terminology
PLOT: Used for creating a 2D line plot of an equation or data.
SCATTER: Used for creating a scatter plot of data points.
ROOTS: Calculates the roots of a polynomial given its coefficients.
POLYVAL: Evaluates a polynomial for given x-values.
POLYFIT: Fits a polynomial to a data set using least squares.
FIND: Identifies positions of specific values in a vector or matrix.
Page 2: Basic Plotting Syntax
Two built-in plotting functions:
plot(x,y): For plotting a line plot.
scatter(x,y): For creating scatter plots.
Requirements:
xandymust be vectors with equal length.
Page 3: Help Documentation
Access help documentation via:
help plotorhelp scatterfor formatting syntax.
Remember:
Familiarize yourself with using resources instead of memorization.
Page 4: Related Plotting Functions
figure: Opens a new figure window.
figure(n): Opens a figure window numbered "n".
hold on: Allows additional plots on the current figure.
hold off: Stops holding the current plot (replacement occurs on next plot command).
Page 5: Formatting Plot Elements
axis([xMIN xMAX yMIN yMAX]): Sets the limits for the axes.
title('string'): Adds a title to the plot.
xlabel('string') and ylabel('string'): Adds labels for x and y axes respectively.
legend('String 1', 'String 2', ...): Adds a legend for different data sets displayed.
Page 6: Plot Formatting Essentials
Minimum requirements for proper plots:
Appropriate type of data (theoretical vs. experimental).
Correct axes values for data display.
Axes labels including name and units.
Legend if multiple data sets are included.
Page 7: Polynomials in MATLAB
Polynomials are represented by coefficient vectors in MATLAB.
Example polynomial: 8x^4 + 3x^3 + 5x + 29 is represented as:
p = [8 3 0 0 5 29].
Page 8: Finding Roots of Polynomials
ROOTS syntax:
R = roots(P).Ris the output vector of roots.Pis the coefficients vector.
Example:
C_VEC = [1 -5 -6];yields rootsR = [3.0000 2.0000].
Page 9: Evaluating Polynomials
POLYVAL syntax:
Y = polyval(P,x).Y=output y-values for polynomial defined by coefficientsPfor x-values inx.polyval = polyval function
P = vector that contains coefficients in order
x = vector of x-values where you are calculating the corresponding y-values
Select enough x-values for smooth plotting.
Page 10: Fitting Polynomials to Data
POLYFIT syntax:
P = polyfit(x,y,n).Pholds the coefficients for the fitted polynomialpolyfit = polyfit function
x= vector containing the independent variable data
y = vector containing the dependent variable
nis the polynomial order
Significance:
Allows estimation of values between collected data points.
Page 11: Using POLYFIT
Steps to use POLYFIT:
Plot the data initially.
Determine the polynomial order based on data trends.
Apply
polyfitto get coefficients.Plot the polynomial fit to evaluate accuracy.
Aim for using the lowest polynomial order to prevent overfitting.
Page 12: Example Polynomial Fits
Example outputs for various orders using POLYFIT:
First Order:
FIRST = polyfit(x,y,1)results in coefficients:4.4108, 45.6319.Second Order:
SECOND = polyfit(x,y,2)results in coefficients:-0.3686, -1.4869, 61.7277.Third Order:
THIRD = polyfit(x,y,3)results in coefficients:0.0594, 1.0559, -1.5462, 0.4738.
Page 13: Finding Elements in Vectors
-Returns the index positions where specific values in a vector or matric occur
-logical operators used are <,>,==,~=,>=,<=
FIND syntax:
Syntax #1:
INDEX=find(X)for linear indices.Syntax #2:
[R,C]=find(X)for row and column indices.
Examples:
A>10,A==10,A>10 & A<20are logical expressions evaluated to find positions.