How do I make my graph print with a line of fit (red solid line) going up, and also 95% confidence bands.
1 view (last 30 days)
Show older comments
%These commands clear the workspace and command window, in that order.
clear
clc
%Makes long numbers
format long g
%Density Values
d=[4879 12104 12756 6792 142984 120536 51118 49528]';
%Gravity Values
g=[3.7 8.9 9.8 3.7 23.1 9.0 8.7 11.0]';
%Meters converstion
D= d.*1000;
%Gravitational Constant
G=6.674e-11;
p=3.*g./(2*pi.*g.*D)
X=D;
Y=g;
%Makes matrix one column & References rows.
G1=X;
G1(:,2)=ones(length(X),1);
delta=G1\Y;
%Slope of line
slope=delta(1,1)
%Y-intercept
intercept=delta(2,1)
%Creates line of figure graph.
X1=[0:1e-3:15*10^4]';
Y1=delta(1,1).*X1+delta(2,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is a certain type of figure Matlab will draw.
figure(1)
%Clears existing graphs.
clf
%Plots command and bold blue dots.
plot(X,Y,'ob','MarkerFaceColor','b','Markersize',7)%plot the data
%Finished plotting.
hold off
%Sets background color to white.
set(figure(1),'color','white')
%labels for the x and y axis.
xlabel('D(m)')%label the x axis
ylabel('g(m/s^2)')%label the y axis
%Average density of the earth
PE=5496.25342379935;
DS=120536.*1000;
gs=(2*pi.*G1.*DS.*PE)./3
0 Comments
Answers (1)
Image Analyst
on 28 Nov 2018
Add this to the end of your code:
% Fit a line.
coefficients = polyfit(X, Y, 1);
% Get 1000 x values
xFit = linspace(min(X), max(X), 1000);
% Get fitted y values.
yFitted = polyval(coefficients, xFit);
% Plot
hold on;
plot(xFit, yFitted, 'r-', 'LineWidth', 3);
grid on;
0 Comments
See Also
Categories
Find more on Graphics Object Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!