Re: code to create & fill matrixes of varying sizes




"Jeff G" <matlabstudent@xxxxxxxxxxxxx> wrote in message
news:fh63li$3jq$1@xxxxxxxxxxxxxxxxxxxxx
I am creating a m-file to do polynomial interpolation. In
it, a user inputs two vectors (x-points in one vector,
y-points in the other) and specifies to which polynomial
degree they would like the program to interpolate.

I have a working program that will work for polynomials to
the 10th degree, but it relies heavily on an if/elseif
statement to apply the vectors to the correct code. But is
10 pages long (much of it is repeatable so cutting & pasting
makes it less cumbersome).


Here&#8217;s a snippet:

sy=sum(y);
sx=sum(x);
sx2=sum(x.^2);
sx3=sum(x.^3);
sx4=sum(x.^4);
sx5=sum(x.^5);
sx6=sum(x.^6);
sx7=sum(x.^7);
sx8=sum(x.^8);
sx9=sum(x.^9);

*snip*

In addition to John's comments elsewhere in this thread, this is a perfect
example of why you shouldn't try to create variables numbered like this. It
tends to bloat the code. If you have to do polynomial interpolation like
this (say it's a class assignment) and x and y are vectors of the same size,
this is much more compact:

x = -3:1:3;
y = x;
n = 10;
m = 10;
S = zeros(n+1, m+1);
for a = 0:n
for b = 0:m
S(a+1, b+1) = sum((x.^a).*(y.^b));
end
end

Where before you would have used something like sx4y6 in your equations, now
you'll use S(5, 7).

--
Steve Lord
slord@xxxxxxxxxxxxx


.



Relevant Pages

  • Questions on polynomial interpolation
    ... I am a physics PhD student. ... to introduce an `interpolation variable', i.e. to introduce a function $g$ and interpolate the modified set. ... which is much easier to do with polynomials. ... I have already consulted various books, including Powell, Approximation theory and methods, but I couldn't find precise answers. ...
    (sci.math.num-analysis)
  • Re: How to find the area of 2 polynomials?
    ... difficulty to find the area of 2 different polynomials. ... the area bounded by 2curves(the polyfit curve and the original ... Simplest is to interpolate both curves ... want the exact solution, it is not too ...
    (comp.soft-sys.matlab)
  • Re: Interpolating coordinates problem
    ... > followed by a car, where ti is the time instant when the car was at ... > is to extrapolate the location for a time instant t>tn. ... > Points to interpolate: ... > Polynomials to use for the interpolation: ...
    (sci.math)
  • Re: Equation of a curve
    ... an infinite number of possible functions that interpolate ... are also spline models one can apply for least squares ... Polynomials are actually in this class, ... The best material model of a cat is another, or preferably the same, cat. ...
    (comp.soft-sys.matlab)