--%>

implement a user-defined matlab function named lsquares

Your solution to the problem should be housed within a while loop, which allows the grader to test your solution repeatedly without having to re-execute the script, as shown in class. You should first display the purpose of the program, and then you should prompt the user if he/she wishes to run the code. If the user responses 'y' or 'Y', you should then run the actual problem solution (whatever that entails). After running your solution, you should prompt the user if he/she wishes to run it again, and if so, do so. Otherwise, exit the program with a closing goodbye statement.

In this final project you will implement a user-defined matlab function named lsquares with the following "prototype":

function [m,b,e,s,r] = lsquares(x,y)

which computes the least squares regression line fit to the data whose coordinates are in the arrays x and y. The output arguments m and b are always computed and returned, but the user has the option to get e,s and r, if he/she so desires. If these outputs are not requested, your code in your function should NOT compute them.

You will code up the function lsquares in the .m file "lsquares.m", and also a driver program which performs the following steps (as long as the user wants to do so):

1) Prompt the user for a plain text data file, which has 4 columns of data: the first column is string data, the remaining 3 are numeric.

2) Read the second, third, and fourth columns into the variables x,y,and y_true, respectively.

3) Prompts the user for whether he/she wants to see the correlation coefficient r.

4) Computes the least squares fit of the data. You are to obtain r only if the user so desired. On the same plot, plot the x vs. y data with red stars, x vs. the least squares fit with solid blue line, and x vs. y_true with a solid green line. You should output to the user the resulting slope, intercept, and r value (only if the user requested that piece of information).

EXTRA CREDIT: In addition to using your function, lsquares, to compute the least squares fit, use the built-in matlab function polyfit to find the coefficients m and b. Display matlab's m and b values and compare them to yours.

The formulae for m,b,e,s, and r were given in class, and are also given in the file "formula.txt", which is in the inclass_code/fall_2010 subdirectory.

A test file is available: in the inclass_code/fall_2010 subdirectory, which has data of the form you will be loading: "project4.txt". The true slope is 2.5, the true intercept is -1. The regression slope should work out to be 2.4491, and the regression intercept -.7932

   Related Questions in Financial Accounting

  • Q : Maintaining the fixed exchange rate

    Explain why “Once  the capital markets are integrated, it becomes difficult for the country in order to maintain the fixed exchange rate”. 

  • Q : APV capital budgeting framework How APV

    How APV capital budgeting framework is useful for analyzing the foreign capital expenditures?

  • Q : Major reasons of current account

    In contrast to the U.S., Japan has observed constant current account surpluses. What would be the major reasons for such surpluses? Is it advantageous to have constant current account surpluses?

  • Q : Prepare adjusting journal entries The

    The following information for the month of December 20x6, with respect to cash activities, was gathered by Tressa Ltd.’s bookkeeper. Cash balance per books, December 1 $ 3,700

    Q : Screening of Elder Abuse What is the

    What is the aim of the research in the screening of elderly abuse at the primary health care level ?

  • Q : Political events affecting the local

    Discuss about the different ways in which the political events in the host country affects the local operations of MNC.

  • Q : Benefits for a company to cross-list

    Explain the benefits you can think of for a company to (a) cross-list its equity shares on more than one national exchange, and, (b) to source new equity capital from foreign investors as well as domestic investors.

  • Q : Bankruptcy A legal process that allows

    A legal process that allows a debtor, either a person or a business, to redundant some or all of the debt. The normal process involves selling asset and using the proceeds to pay off creditors in an order and/or in an amount determined by a judge. Some types of bankru

  • Q : Direct loans-loan guarantees and credit

    Do you think that government of the country must assist the private business in conduction of the international trade through the direct loans, loan guarantees, and/or credit insurance?