Working out an equation on MATLAB using co-ordinates

  • MATLAB
  • Thread starter Saints-94
  • Start date
  • Tags
    Matlab
In summary, the data points plotted on the graph are fitted to a quadratic function using the polyfit() routine in MATLAB.
  • #1
Saints-94
63
1
I have a series of data points for X and Y points on a graph. The data is quite random and I am trying to work out a trend line so I can then form an equation for the line. How would I go about working out the equation for the data below using MATLAB?
(0, 580)
(6.7, 495)
(13.4, 445)
(18.7, 365)
(22.8, 350)
(27, 340)
upload_2017-3-6_20-7-31-png.114188.png

This is the data that has been plotted on Excel.
 
Physics news on Phys.org
  • #2
Matlab:
x = [0 6.7 13.4 18.7 22.8 27]
y = [580 495 445 365 350 340]
 
  • #3
I managed to plot the points using that coding, but I am struggling with drawing a trend line and working out an equation.
 
  • #4
Did you look at the link I gave you in your other thread on how to do a fit using Matlab?
 
  • #5
Yes, I read through the link. However I was struggling to understand the MATLAB coding, hence why I have asked for further MATLAB coding help.
 
  • #6
Try this:

Code:
% Initialization steps.
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
clear;  % Erase all existing variables. Or clearvars if you want.
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;

x = [0 6.7 13.4 18.7 22.8 27]
y = [580 495 445 365 350 340]
% Demo to illustrate how to use the polyfit routine to fit data to a polynomial
% and to use polyval() to get estimated (fitted) data from the coefficients that polyfit() returns.
% Demo first uses a quadratic fit via polyfit()%============= QUADRATIC FIT ===================================
% Now we have sample, noisy y values that we will fit a curve through.

% Plot the training set of data (our noisy y values).
plot(x, y, 'ro', 'MarkerSize', 8, 'LineWidth', 2);
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Linear Fit', 'FontSize', fontSize);

% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

% Do the regression with polyfit.  Fit a quadratic curve through the noisy y values.
coefficients = polyfit(x, y, 2)
% Make fit.  It does NOT need to have the same
% number of elements as your training set,
% or the same range, though it could if you want.
% Make 500 fitted samples going from the min x to the max x.
xFit = linspace(min(x), max(x), 500);
% Get the estimated values with polyval()
yFit = polyval(coefficients, xFit);
% Plot the fit
hold on;
plot(xFit, yFit, '.-', 'LineWidth', 2);
legend('Training Set', 'Fit', 'Location', 'Northeast');
 
  • Like
Likes Saints-94
  • #7
That's great, thanks for your help! Is there anyway to show the equation for the 'fit' line?
 
  • #8
Saints-94, you can do

fprintf('y = %f * x^2 + %f * x + %f\n', coefficients(1), coefficients(2), coefficients(3));
 

Related to Working out an equation on MATLAB using co-ordinates

1. How do I input coordinates into MATLAB to work out an equation?

To input coordinates into MATLAB, you can use the "plot" function, which takes in a set of x and y coordinates and plots them on a graph. Alternatively, you can create arrays for the x and y values and use the "plot" function on those arrays.

2. Can I use both Cartesian and polar coordinates in MATLAB?

Yes, MATLAB has functions for converting between Cartesian and polar coordinates, such as "cart2pol" and "pol2cart". This allows you to work with both types of coordinates in your equations.

3. How can I plot multiple equations with different coordinates on one graph?

You can use the "hold" function in MATLAB to plot multiple equations on the same graph. This allows you to plot each equation separately, using the "plot" function, and they will all appear on the same graph.

4. Can I use matrices to represent coordinates in MATLAB?

Yes, you can use matrices to represent coordinates in MATLAB. You can create a matrix with two columns, one for the x coordinates and one for the y coordinates, and use this matrix in place of separate arrays for the coordinates in your equations.

5. Is there a way to label the coordinates on a graph in MATLAB?

Yes, you can use the "xlabel" and "ylabel" functions in MATLAB to label the x and y axes on your graph. You can also use the "title" function to add a title to your graph.

Similar threads

Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top