Re: code to create & fill matrixes of varying sizes
- From: "Steven Lord" <slord@xxxxxxxxxxxxx>
- Date: Mon, 12 Nov 2007 00:40:06 -0500
"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’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
.
- References:
- code to create & fill matrixes of varying sizes
- From: Jeff G
- code to create & fill matrixes of varying sizes
- Prev by Date: text labels for the x-axis
- Next by Date: POLY2CW ??? what happened to it?
- Previous by thread: Re: code to create & fill matrixes of varying sizes
- Next by thread: Re: code to create & fill matrixes of varying sizes
- Index(es):
Relevant Pages
|