Least Squares Function Error

I want to write a function called LeastSquares that determines the case of the solution to Ax = b. In addition, fhe function also solves for x accordingly and returns the norm of the error vector. But, I cannot figure out what is wrong with my code. Thanks for the help!
A: a matrix of arbitrary size.
b: a column vector, which has the same number of rows as matrix A.
method: should be filled with either ’mldivide’ or ’pseudoinverse’. The method that you specify will be used to solve for the cases where non-unique solutions exist. If method is not specified, the default method should be ’mldivide’. The outputs for this function are as follows:
xstar: the solution to Ax = b.
cstar: the norm of the error vector.
flag is a string, with the following values, depending on the type of solution:
– Unique Exact Solution: flag is ’1-Unique Exact Solution’
– Non-Unique Exact Solutions: flag is ’2-Non-Unique Exact Solutions’
– Unique Least Squares Solution: flag is ’3-Unique Least Squares Solution’
– Non-Unique Least Squares Solutions: flag is ’4-Non-Unique Least Squares Solutions’
Code:
function [flag xstar cstar] = LeastSquares(A,b,method)
size(A)
rank(A)
rank([A b])
flag= disp ('1- Unique Exact Solution')
size(A)
rank(A)
rank([A b])
xtar = A\b
flag=disp ('2-Non-Unique Exact Solution')
size(A)
rank(A)
rank([A b])
xstar_1 = A\b
c_1 = norm(A*xstar_1-b)
xstar_2 = pinv(A)*b
flag= disp('3-Unique Least Squares Solution')
size(A)
rank(A)
rank([A b])
xstar = A\b
cstar = norm(A*xstar)
cstar = pinv(A)*b
cstar = norm(A*xstar)
norm(xstar_1)
norm(xstar_2)
flag=disp('4 Non-Unique Least Squares Solution')
Sample Test Case
>> A = [1 2 3;2 4 8;3 5 7];
>> b = [22;54;53];
>> [flag xstar cstar] = LeastSquares(A,b)
flag =
1-Unique Exact Solution
xstar =
1.0000
3.0000
5.0000
cstar =
0
>> A = [1 5;2 10;3 15];
>> b = [10;20;30];
>> [flag xstar cstar] = LeastSquares(A,b,'mldivide')
flag =
2-Non-Unique Exact Solutions
xstar =
0
2.0000
cstar =
1.6281e-014
>> A = [1 2 3;2 4 6;3 6 9];
>> b = [22;54;77];
>> [flag xstar cstar] = LeastSquares(A,b,'pseudoinverse')
flag=
4-Non-Unique Least Squares Solutions
xstar =
1.8418
3.6837
5.5255
cstar =
4.5119

9 Comments

Matt J
Matt J on 19 Oct 2012
Edited: Matt J on 19 Oct 2012
Ping
Ping on 19 Oct 2012
Edited: Ping on 19 Oct 2012
it's definitely the same question! Guess we're in the same class then.... But it's good to know i'm not the only who's lost though haha
Edit: Matt, I just checked out your hints, and I am using rank, but why am i still getting an error?
You are computing rank, but you are not analyzing the result in any way. Your code is also not analyzing the 'method' input argument to determine what solver is to be used.
but since method is supposed to be mldivide (which is the backslash) do I declare it like: method=./ ? I don't understand the point of having "method" in the code?
why would you use the tag 'idiot' for this function?
I didn't put it there and I can't remove it for some reason :( I guess my attempt was really terrible/idiotic...
I can remove the ‘idiot’ tag and I did.
Asking for help is not idiotic. Ping seems to be making an honest attempt. At least this is not a ‘send me the code’ post!
Matt J
Matt J on 19 Oct 2012
Edited: Matt J on 19 Oct 2012
I didn't put the idiot tag there, but I do suspect that it's not an honest attempt. The problem is almost identical to the one I provided a link to earlier except that Ping's code includes a lazy-looking effort to incorporate the hints I gave there.
I can't help but wonder whether the 2 posts are by the same person under different aliases, and if Ping is fishing for a more complete solution than the hints I initially gave. Even if they are not the same person, I gave the same hints to Ping in this similarly-themed question
Star Strider
Star Strider on 19 Oct 2012
Edited: Star Strider on 19 Oct 2012
I tend to agree that there's not a lot of information provided in the post as to what isn't working, or what the OP has done to correct the problems. When I looked up the link you provided, ‘Ping’ was the OP there as well so you're correct that they're by the same person. This is certainly not the first time someone has repeatedly posted the same question.
That said, ‘Ping’ simply needs to experiment with the code and read the core MATLAB documentation on program flow control and data classes to figure out what's necessary to meet the requriements of the homework assignment.
I decided not to get involved in the substance of this because it was obvious I couldn't add anything of significance to what you're already doing. However the ‘idiot’ tag caught my attention.

Answers (0)

This question is closed.

Asked:

on 19 Oct 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!