polynomials with increasing order

hi, i want to create an equation of this format x^1+x^2+x^3........x^n. how can i do that?

Risposte (1)

dpb
dpb il 24 Mag 2016
doc polyval
NB: For higher orders numerics for polynomials get bad quickly. You can help somewhat by using the standardization technique documented in polyfit

4 Commenti

major problem is how to give the command such that matlab computes the x^1+ x^2 to x^n. thats important since i want to be able to input n as any number . if n was known i could write the x orders until i get to the highest, but i want it such that n could be anything
n = 5;
x = zeros(1, n);
Now fill in x. But note that if you're using dpb's suggestion, the polynomial functions in MATLAB store coefficients in decreasing order not increasing. So this:
p = [1 2 3];
is 1*x^2 + 2*x + 3 not 1 + 2*x + 3*x^2.
result = polyval([1 2 3], 5)
y1 = 1*5^2 + 2*5 + 3
y2 = 1 + 2*5 + 3*5^2
If x is a vector instead of a scalar, make sure you use dot caret instead of caret.
dpb
dpb il 24 Mag 2016
Modificato: dpb il 24 Mag 2016
For the specific form outlined with all unity coefficients (and actually for any as Steven shows), Matlab already has the function--it's polyval. Ignoring the numerics issue on the hope you'll restrict [N,x] to reasonable values it's simply
N=4;
x=0.3;
y=polyval(ones(N,1),x);
It's simple enough to rewrite this just a little via a function handle so only the order and x is required
>> poly=@(n,x) polyval(ones(n,1),x);
>> n=3;
>> x=[0:0.1:0.4];
>> poly(n,x)
ans =
1.0000 1.1100 1.2400 1.3900 1.5600
>>
NB: it's already vectorized as well.
You can deal with the issue of coefficients for the terms and even the order of the terms(*) if desired similarly.
(*) If you're adamant you want the order to be from constant to increasing power-of-x, then
poly=@(n,x) polyval(fliplr(ones(n,1)),x);
IOW, your function poly simply swaps the order in which it passes the coefficients to polyval--it's transparent to the use later. Of course, you have to be consistent and remember you can't pass the coefficient vector directly to the Matlab function.

Accedi per commentare.

Categorie

Scopri di più su App Building in Centro assistenza e File Exchange

Modificato:

dpb
il 24 Mag 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by