As you may recall from your Math courses, the equation of a line is: y = m * x +
ID: 2267940 • Letter: A
Question
As you may recall from your Math courses, the equation of a line is:
y = m * x + b
Given the set of data points, our task is to find the values of m and b which describes the line which fits "best" to this set of data. Equations for m and b can be derived, but here, I just give them to you:
Your task is to write a program that reads the data points redirected from a file (i.e. no prompts), computes the equation of the best fit line, and produces output that we can use to plot the results. We can use MATLAB to plot the result. You do not need to know MATLAB, you just need to produce a redirected output file that looks exactly like:
p = [ 0.00, 20.00, 60.00, 68.00, 77.00, 110.00]; x = linspace( 0.00, 5.00, 6); y = 20.83 * x + 3.76; plot(x, y ,x ,p, 'go') text(0.5,100,'y = 20.83 * x + 3.76');
Note, the red text above will be different for different data files; the black text will always be the same.
I have provided a data file for the above set of points in the ~ee160/Homework/Hw1 directory called temps.dat. Your program should produce a file called graph.m, which is a file MATLAB will recognize to show your results. You will run your program with a command like:
a.out < temps.dat > graph.m
Once you have a correct graph.m file, you can see your plot by transferring your file to a peecee with MATLAB and run the .m file there. We will only be looking at the format of your output file.
Your program should produce the correct equation for the best fit line and the correct MATLAB file for any set of data. For example, try removing any of the points in the above data file, and you will get a different line and a different plot. You should call the source code file for your program regress.c.
Explanation / Answer
Dear friend, I could not find the temp.dat data file on the mentioned link. However, I have created a temp.dat file for your reference and wrote a matlab script that is required in the problem. You amy just replace the contents of the temp.dat file with your data set and run the script. There can be 'N'' no. of data points. The first column is X and second column is Y.
The Temp.Dat file used is given below:
******************************************
1 3
2 2.5
3 4.2
4 5.3
5 6.6
6 7.2
7 8.3
8 9.0
9 10.2
10 10.9
******************************************
The matlab script is given below:
************************************************************************
close all,
clear all,
clc,
ProjectPath = pwd;
TempFile = strcat(ProjectPath,' emp.dat');
A=[];
fpt = fopen(TempFile,'r');
A = fscanf(fpt,'%g');
x = length(A)
No_of_Data = x(1,1);
fclose(fpt);
A
X=[];
Y=[];
Cnt=1;
for i=1:2:x
X(Cnt) = A(i);
Y(Cnt) = A(i+1);
Cnt=Cnt+1;
end
X
Y
PolyOrder=1;
p = polyfit(X,Y,PolyOrder);
f = polyval(p,X);
plot(X,Y,'o',X,f,'-')
xlabel('---- Conc. (mg/ml) --->');
ylabel('---- Intensity --->');
str = strcat('Plot between Intensity and Concentraion at Poly Order = ',num2str(PolyOrder));
title(str);
PolyOrder=1;
P = polyfit(X,Y,PolyOrder);
f = polyval(p,X);
subplot(1,2,1); plot(X,Y,'o',X,f,'-');
xlabel('---- X --->');
ylabel('---- Y --->');
str = strcat('Plot between Y and X at Poly Order = ',num2str(PolyOrder));
title(str);
Blank = ones(400,350);
subplot(1,2,2); imshow(Blank);
str = strcat('Y = ',num2str(P(1,1)),'*X + ',num2str(P(1,2)));
text(30,100,str);
************************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.