Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    13
    ReputationRep:
    I'm trying to do the polynomial curve-fitting part of this pdf (attached). I'm having trouble calculating the coefficients. I get no 0th order coefficient. I'm also having a lot of trouble with the ' \ ' operator because even when the length of v and A match up (or should do) it says the matrix size is wrong.

    This is my code
    Code:
    function[] = C002v2_2() 
        start = 0;
            fin = 200; 
         n = 100; 
         div = (fin - start)/n; 
         c1 = [1.5, -2.5, 0.7, -1.2]; 
        e = 0.01; 
        x = start:div:fin; 
        y = x; 
        for i = 1:n 
            y(i) = c1(1) + c1(2) * x(i) + c1(3) * x(i)^2 + c1(4) * x(i)^3 + e * randn(); 
        end
                v = zeros(4,1); 
        for k = 1:4 
            for i = 1:n 
                v(k) = v(k) + y(i)*x(i)^k; 
            end 
        end 
        A = zeros(4, 3); 
        for k = 1:4 
            for i = 1:3 
                for j = 1:n 
                    A(k,i) = A(k,i) + x(j)^(i+k); 
                end 
            end 
        end 
            %A 
         %v 
                c2 = A \ v; 
         %c2
        yf = c2(1) + c2(2) * x + c2(3) * x.^2 + c2(4) * x.^3; 
        f = figure(); 
        plot(x, y, x, yf); 
    end
    Thanks so much. I'm desperate.
    Attached Images
  1. File Type: pdf C002.pdf (188.2 KB, 116 views)
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Would you like to hibernate through the winter months?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.