Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views5 pages

Matlab GraphingBasics Guide

Matlab graphing code

Uploaded by

afrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Matlab GraphingBasics Guide

Matlab graphing code

Uploaded by

afrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Generating Graphs

% equations and relations can be graphed and altered using the following
% format, given that a relationship has been established.

% Consider the quadratic equation and arrays to plot a basic graph

x = linspace(1,20,20);

y = 2.*(x.^2) + 2;

plot(x,y) %graph using data


xlabel("these are the x values") %adds x axis label
ylabel("these are the y values") %adds y axis label
title("Graph Title") %adds graph title

%graphs can be modified aesthetically by adding aesthetic preferences after


%after the variables in plot operator. For example plot(x,y,'LineWidth', 3)
%adjusts line width. A full list of aesthetic modifiers can be found on
%Mathworks

1
%multiple graphs can be added to the same set of axis.

x = [1 2 3 4 5 6 7 8 9];
y = (x).*0.5;

x1 = [1 2 3 4 5 6 7 8 9];
y1 = (x1).*0.25;

plot(x,y,"*", x1, y1,"o") %graph using data sets on same axis, aesthetics are added after the (
xlabel("these are the x values") %adds x axis label
ylabel("these are the y values") %adds y axis label
title("Graph Title") %adds graph title

legend("blue","red") % a legend can be made to differenciate lines. The order the labels appear

% natural log and log graphs can be plotted using the following

m = linspace(1, 10, 10)

m = 1×10
1 2 3 4 5 6 7 8 9 10

n = m.^2

2
n = 1×10
1 4 9 16 25 36 49 64 81 100

semilogy(m,n, 'o') % plots ln(y)/x

semilogx(m,n,'*') % plots y/ln(x)

3
% Scatter plots can be made out of two data sets

independentx = [ 1 2 3 4 5 6 7 8 9 10]

independentx = 1×10
1 2 3 4 5 6 7 8 9 10

dependenty = [ 0.2, 0.23, 0.3, 0.4, 0.38, 0.45, 0.55, 0.57, 0.61, 0.67]

dependenty = 1×10
0.2000 0.2300 0.3000 0.4000 0.3800 0.4500 0.5500 0.5700

scatter(independentx,dependenty,'.') %plots scatter plot using x and y values


title("scatter plot")

hold on

% the line of best fit can be added using polyfit and polyval

% using polyfit specifies the highest coefficient of the polynomial eqn


% that is the line of best fit, written using coefficients=polyfit(x,y,n)
% were n is the highest coefficient of the polynomial. n=1 gives straight
% line

4
coefficients = polyfit(independentx,dependenty, 1); %gets coefficients of line of best fit

xFit = linspace(min(independentx), max(independentx), 10); % creates new x axis with 100 point

yFit = polyval(coefficients , xFit); % approximates y fit values for new x points

plot(xFit, yFit) % this plot gives us the line of best fit

%to scale graphs(for clarity etc.)

xlim([0,12])
ylim([0,1])

hold off

legend("points", "line of best fit")

You might also like