Vandermonde Matrix and an Error Vector

AI Thread Summary
The discussion focuses on calculating an error vector using a Vandermonde matrix in MATLAB. Users are advised to use the mldivide operator for solving equations of the form VX = B and to ensure that B is a column vector to avoid dimension errors. The provided code demonstrates how to input a series of numbers, construct the Vandermonde matrix, and compute the solution X and the residual error. The final results show that the calculations yield a very small residual, indicating a good fit. This method effectively addresses the problem of solving for X in the context of error analysis.
Siann122
Messages
37
Reaction score
0
format long

n = inputdlg('Please enter a series of numbers separated by spaces/commas: ')
numbers = str2num(n{1});
X = 0
V = fliplr(vander(numbers))
r_size = size(V,2);
c_size = size(V,1);
B = 1+(r_size./numbers)
X = B/V

Here's what I have so far. I've been running the program with n = 5, 15, 20, and basically I want to implement an a way of calculating an error vector using Error = (X - Dt), where Dt = [1, 1, 0, 0, 0, . . . , 0]T.

I'm not really quite sure how to go about this, and is this a good way to solve VX = B for X? Cheers for any advice guys.
 
Physics news on Phys.org
You should use mldivide, \, when solving equations of the form VX = B. Use mrdivide, /, when the form is XV = B.

Replace that line with X = V\B

You can find the residual error from r = B - V*X
 
kreil said:
You should use mldivide, \, when solving equations of the form VX = B. Use mrdivide, /, when the form is XV = B.

Replace that line with X = V\B

You can find the residual error from r = B - V*X

When I use that I get an error saying Matrix Dimensions Must Agree?
 
That's just because B is a row vector instead of a column vector. Try this:

Code:
n = inputdlg('Please enter a series of numbers separated by spaces/commas: ')
numbers = str2num(n{1})'; % added a ' to make it a column vector
X = 0;
V = fliplr(vander(numbers))
r_size = size(V,2);
c_size = size(V,1);
B = 1+(r_size./numbers)
X = V\B

For n = 5,10,15,20 I get:

Code:
n = 

    '5,10,15,20'V =

           1           5          25         125
           1          10         100        1000
           1          15         225        3375
           1          20         400        8000B =

    1.8000
    1.4000
    1.2667
    1.2000X =

    2.6667
   -0.2333
    0.0133
   -0.0003

The residual is

Code:
r = norm(B-V*X)

r =

   3.8459e-16
 
kreil said:
That's just because B is a row vector instead of a column vector. Try this:

Code:
n = inputdlg('Please enter a series of numbers separated by spaces/commas: ')
numbers = str2num(n{1})'; % added a ' to make it a column vector
X = 0;
V = fliplr(vander(numbers))
r_size = size(V,2);
c_size = size(V,1);
B = 1+(r_size./numbers)
X = V\B

For n = 5,10,15,20 I get:

Code:
n = 

    '5,10,15,20'


V =

           1           5          25         125
           1          10         100        1000
           1          15         225        3375
           1          20         400        8000


B =

    1.8000
    1.4000
    1.2667
    1.2000


X =

    2.6667
   -0.2333
    0.0133
   -0.0003

The residual is

Code:
r = norm(B-V*X)

r =

   3.8459e-16

Thanks very much, this works like a charm.
 
Back
Top