To be done in fortran please. ME021 Engineering Computing -Spring 2018 Assignmen
ID: 3722118 • Letter: T
Question
To be done in fortran please. ME021 Engineering Computing -Spring 2018 Assignment Fortran Project Due the week of Mar S, last lab session Least Squares Line Fit When given a set of data that looks like a line, engineers often want to find the ymx+b line that best fits the data. In other words, when given a list of x values and a list of corresponding yvalues, we want to find the slope m and intercept b of the line that comes closest to the x and y data points This is actually a straightforward process via the method of least squares", based on minimizing the error between the actual data points and the line one gets using certain values of m and b. We will not cover the derivation here, but the results we will use are below . Assume that each of our x values is called x and each of our y values is called y, so that a single data point is (x, y). Igoes from 1 to n, which is the number of data points we have. We define average x and y values: Zo yi Then we find the best-fit slope to be Tavg and the best-fit intercept to be Project Overview: Our project will be to get a file name from the user, read x and y data from file, display the data at th¢ console, allow the user to remove points from the data, then find the best-fit slope and intercept and send them to the console. In more detall, the project program you write will do the following Prompt the user for a file name, which will be stored in a string variable. Use the ADVANCE-"no" in the write statement for the prompt to get the prompt and the user's response on the same line. . Read the x and y data from the file and store it in two dynamic, double-precision real arrays. This will require counting the lines in the file to see how many data lines there are and then allocatingExplanation / Answer
As my best of Knowledge and working Experience the below is the desired code
parameter(SIZE=25)
integer i,ij,j,k,n,n1,m,m1,m2
real*8 C(SIZE,SIZE)
real*8 A(SIZE),B(SIZE),X(SIZE),Xc(SIZE),Y(SIZE),Yx(SIZE)
real*8 p,xx,s,yc
write(*,10,advance='no'); read *, n
n=n-1
write(*,20,advance='no'); read *, m
n1=n+1; m1=m+1; m2=m+2
print *,' '
print *,' Function to approximate:'
do i=1, n1
if (i<10) then
write(*,30,advance='no') i, i
else
write(*,31,advance='no') i, i
end if
read *, X(i), Y(i)
end do
do k=1, m2
Xc(k)=0.d0
do i=1, n1
Xc(k) = Xc(k) + X(i)**k
end do
end do
yc=0.d0
do i=1, n1
yc = yc + Y(i)
end do
do k=1, m
Yx(k)=0.d0
do i=1, n1
Yx(k) = Yx(k) + Y(i)*X(i)**k
end do
end do
do i=1, m1
do j=1, m1
ij=i+j-2
if (i==1.and.j==1) then
C(1,1) = n1
else
C(i,j)=Xc(ij)
end if
end do
end do
B(1)=yc;
do i=2,m1
B(i)=Yx(i-1)
end do
do k=1, m
do i=k+1, m1
B(i) = B(i) - C(i,k)/C(k,k)*B(k)
do j=k+1, m1
C(i,j) = C(i,j) - C(i,k)/C(k,k)*C(k,j)
end do
end do
end do
A(m1)=B(m1)/C(m1,m1)
do i=m, 1, -1
s=0.d0
do k=i+1, m1
s = s + C(i,k)*A(k)
end do
A(i) = (B(i)-s)/C(i,i)
end do
print *,' '
write(*,40) m, n+1
print *,' Coefficients of polynomial:'
do i=1, m1
write(*,50) i-1, A(i)
end do
print *,' '
print *,' Approximated function:'
print *,' X Y '
do i=1, n1
xx=X(i); p=0.d0
do k=1, m1
p = p*xx + A(m1+1-k)
end do
write(*,60) xx, p
end do
print *,' '
print *,' '
10 format(/' Number of points : ')
20 format(/' Degree of polynomial: ')
30 format(' X(',I1,'), Y(',I1,') = ')
31 format(' X(',I2,'), Y(',I2,') = ')
40 format(' Polynomial approximation of degree ',I2,' (',I2,' points)'/)
50 format(' A(',I,') = ',F15.9)
60 format(2F12.6)
end
! end of file approx.f90
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.