C++ / Matlab Simple Project. ( Easy)

Please see the attached file for details.

Write a MATLAB main .m file and one function m file that executes the following:
a. The main file opens the data file noisedatapa5.txt using fopen and fscanf to read the dc, gt, and snr values into arrays. (You must use the exact file provided with the problem statement including the text headings.) To read lines of text in MATLAB you use the function fgets . (To learn how to use fgets, type help fgets at the command line prompt.)

b. The main m-file uses polyfit, to determine the slopes (a[i]) and intercepts (b[i]) of the best fit lines of snr (y) vs. gt (x) for each value of dc (there are five values of dc). The equation is: snr = a[i]*gt + b[i] for a given dc (You will determine a[i], b[i], for each value of dc, where i ranges from 1 to 5, and is related to the different values of dc.)

c. The main m-file calls a function that you write and sends it the values of gt and snr for each dc value and finds the corresponding rsquared[i] values. The function returns the rsquared[i] values to the main m-file. The main m-file then prints a well-formatted and labeled table to the screen showing the values of slope, intercept and rsquared. (Example below.)

d. The main m-file prompts the user for a design value of snr (call it snrinput), and determines, using the relationships and coefficients determined in part b, an array of gt[i] values that will give the input value of snr for each dc data value. (Or in other words: gt[i]=(snrinput – b[i])/a[i] ; where i ranges from 1 to 5.). The resulting gt[i] values are stored in an array.

e. The main program then uses polyfit again to determine the slope and intercept of the gt[i] values determined in part d vs. the five dc values using this equation:
gt = E1*dc + E2 (Where E1 and E2 are unique for the chosen snrinput).
The main program then writes the values of E1, E2 and the chosen snrinput to the screen in a well-formatted manner.

f. The program then uses the approximate and empirical relationship that cost is proportional to the product of gt and dc to arrive at the optimum cost gt and dc:

derivation follows

cost = k*(dc*gt) (where k is a constant that will cancel out)

substituting in the relationship of gt and dc:
cost = k*(dc*(E1*dc + E2))

Take the derivative of cost with respect to dc, and set it equal to zero:
d(cost)/d(dc) = k*(2*E1 *dc+E2) = 0

solve for
dc = -E2/(2*E1)

substitute this dc of minimum cost into the gt dc relationship using E1 and E2:
gt minimum cost = E1*(dc minimum cost) + E2

These results (dc and gt for minimum cost) are printed to both the screen and the results.txt file in a well-formatted manner.
Reminder: Always display units with numbers when appropriate.

g. The main m-file then plots two figures: one that shows the best fit of the raw data (snr vs. gt) and the derived relationship of gt versus dc for the input value of snr.

Leave a Reply

Your email address will not be published. Required fields are marked *