The file below contains five data points plus additional data. The data is at th
ID: 3785991 • Letter: T
Question
The file below contains five data points plus additional data. The data is at the beginning of the lines, delimited by whitespace. After the data, there will be some explanation as to the meaning of the data. The explanations might not be the same as that shown here, but the order of the lines will always be the same: numbers at the beginning of the line and explanations at the end.
-4 -7.6 Point 1
-2 -17.2 Point 2
0.2 9.2 Point 3
1 -1.6 Point 4
4 -36.4 Point 5
1 xmin
3 xmax
0.2 increment in x
The graph of the function ( ) will pass through all five points if the constants a, b, c, d and e are chosen correctly. After the constants are known, other points along the polynomial can be found by plugging any value of x into the function. For example, an output file could be created containing a table of x and y values that lie on this function:
X Y
1.0000 2.1234
1.2000 3.2834
. . . . . .
3.0000 3.4234
Write a Matlab script to prompt for the input filename, open it and read it. Determine the constants a, b, c, d and e. Construct a vector of x values from xmin to xmax by the increment in x. Determine a y value for each value in the x vector, such that the y values fall on the function. Prompt for an output filename and write your data into the output file. You must have X and Y headings as shown above and the data must be neatly aligned under the headings. Use columns that are 15 characters wide, with 4 digits after the decimal point. Use one space between the columns. Please note that I have an input file with secret numbers that I will use for testing your script. Your script should do its work silently: it will prompt for the filenames, but nothing should be written to the screen after the prompts are issued.
So I did this, is it ok(how would you fix it)
filename=input('enter file name: ','s');
fid=fopen(filename,'r')';
A=fscanf(fid,'%f ',fgetl(fid),[1 inf])';
B=fscanf(fid,'%f ',fgetl(fid),[2 inf])';
C=fscanf(fid,'%f ',fgetl(fid),[3 inf])';
D=fscanf(fid,'%f ',fgetl(fid),[4 inf])';
E=fscanf(fid,'%f ',fgetl(fid),[5 inf])';
xmin=fscanf(fid,'%s',fgetl(fid),1);
xmax=fscanf(fid,'%s',fgetl(fid),1);
increment=fscanf(fid,'%f',1);
fclose(fid);
%X's put in matrix
X1=A(1);
X2=B(1);
X3=C(1);
X4=D(1);
X5=E(1);
matrix=[X1,X2,X3,X4,X5]';
%Y's put in matrix
Y1=A(2);
Y2=B(2);
Y3=C(2);
Y4=D(2);
Y5=E(2);
matrix2=[Y1;Y2;Y3;Y4;Y5];
e = [matrix.^4 matrix.^3 matrix.^2 matrix ones(5,1)];
X=ematrix2;
x_v=xmin:increment:xmax';
xmain = [x_v.^4; x_v.^3; x_v.^2; x_v; ones(1,41)];
Yvalues = X'*xmain;
list=[x_v' Yvalues']
fprinf('%f %f ',list');
Explanation / Answer
Matlab code
clc;clear; % Clearing command window and workspace
filename=input('enter file name: ','s');% input the data file name
fid=fopen(filename,'r'); % open data file for read
while ~feof(fid) % Read data until end of file reach
Line = fgetl(fid); % reading line by line
A = sscanf(Line,'%f ',[1 inf])'; % Exreacting data
if length(A) == 2 % For points
X(str2num(Line(end))) = A(1);
Y(str2num(Line(end))) = A(2);
end
if length(A) == 1
if strcmp(Line(end-3:end),'xmax') % for xamx
xmax = str2num(Line(1:end-4));
elseif strcmp(Line(end-3:end),'xmin') % for xmin
xmin = str2num(Line(1:end-4));
elseif strcmp(Line(end-3:end),'in x') % For step size
h = str2num(Line(1:end-15));
end
end
end
x = xmin:h:xmax; % Creating x vector
p = polyfit(X,Y,4); % Fitting the data
f = @(t) p(1)*t.^4+p(2)*t.^3+p(3)*t.^2+p(4)*t+p(5); % The polynomial function
y = f(x); % Evaluating the function for xmin to xmax
filename=input('enter file name to print result: ','s'); % Output file name
fidw=fopen(filename,'w'); % operning file for writing
fprintf(fidw,'X Y '); % writing x and y heading
for k = 1:length(x) % loop to write the data
fprintf(fidw,'%f %f ',x(k),y(k)); % Writing the data
end
OUTPUT
enter file name: DATA.txt
enter file name to print result: abc.txt
DATA.txt
-4 -7.6 Point 1
-2 -17.2 Point 2
0.2 9.2 Point 3
1 -1.6 Point 4
4 -36.4 Point 5
1 xmin
3 xmax
0.2 increment in x
abc.txt
X Y
1.000000 -1.600000
1.200000 -5.936000
1.400000 -10.753600
1.600000 -15.932800
1.800000 -21.334400
2.000000 -26.800000
2.200000 -32.152000
2.400000 -37.193600
2.600000 -41.708800
2.800000 -45.462400
3.000000 -48.200000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.