-1

I'm trying to build a program to compute the error of the QR method with data points compared to the actual solution. I am already stuck on the basic first draft since the matrix dimensions do not agree and I do not know how to fix this in a way I can still use it as I use it below. Any help on this or general tips on how to build a program for this problem would be greatly appreciated!

Code:

for i=1:21
x(i) = (i-1)/20;
y(i) = x(i)^8;
end

A = makeVandermondeMatrix(x,8)
[Q,R] = qr(A,0);
c = Q' .*y .* inv(R);

where makeVandermondeMatrix is:

function A = makeVandermondeMatrix(x, r)
n = size(x,2);
A = ones(n,r);

for i=1:r+1
    A(:,i) = x.^(r-i+1);
end
p.late
  • 31
  • 6
  • Yes, fixed it now – p.late May 13 '18 at 10:45
  • The vandermonde function works though. that was just posted here for clarity – p.late May 13 '18 at 10:45
  • It does give that error, since that is the first part of the code where an error takes place. You are right about the c, not the y. The y is defined and I changed the line concerning c such that it is correct. – p.late May 13 '18 at 11:20
  • `y` is not defined. Did you mean `y(i) = x(i)^8;` – beaker May 13 '18 at 15:13
  • Okay, so what result do you expect when you perform the element-wise multiplication of a `1x21` vector `y` with a `9x9` matrix `inv(R)`? – beaker May 13 '18 at 18:52

1 Answers1

1

There is already a function in Matlab to generate a Vandermonde matrix. Like the following.

v = 1:.5:3
A = vander(v)

A = 5×5

1.0000    1.0000    1.0000    1.0000    1.0000
5.0625    3.3750    2.2500    1.5000    1.0000

16.0000 8.0000 4.0000 2.0000 1.0000 39.0625 15.6250 6.2500 2.5000 1.0000 81.0000 27.0000 9.0000 3.0000 1.0000

To solve it by the QR method. The following code is necessary as you need to do backsub.

function x = backsub(R,b)
% Backsub for upper triangular matrix.
[m,n] = size(R);
p = min(m,n);
x  = zeros(n,1);

    for i=p:-1:1
        % Look from bottom, assign to vector
        r = b(i);
        for j=(i+1):p
            % Subtract off the difference
            r = r-R(i,j)*x(j);
        end
        x(i) = r/R(i,i);
    end

end

function x2 = genresult(Q,R,b,x)
% Generates result

    b1 = Q'*b;
    x1 = backsub(R,b1);

    x2 = norm(x1-x)/norm(x);

end