How to make and plot a function with a summation?

Goodevening everybody,
I've been trying to make and plot the following function in Matlab:
I have the values for ak in a vector (n is max 100). I've been trying with symsum function and for loops in a sorts of ways but can't get it to work.
I hope someone can help me :)

 Respuesta aceptada

Birdman
Birdman el 11 de En. de 2018
Try this:
n=100;
a=sym('a',[n+1 1]);
syms x k
SmbSum=symsum(a(k+1)*x^k,k,0,n);

7 comentarios

Bas Helfrich
Bas Helfrich el 12 de En. de 2018
Getting the following error:
Error using sym/subsindex (line 796) Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.
For some reason it doesn't take the values out of the vector.
Try the following approach:
n=100;
a=sym('a',[n 1]);
syms x k
eq=sym(zeros(n,1));
for i=1:n
eq(i)=a(i)*x^i;
end
eq=sum(eq)
Bas Helfrich
Bas Helfrich el 12 de En. de 2018
This seems to work, only put in the numers and it should work. Thanks for your help!
Birdman
Birdman el 12 de En. de 2018
You are welcome. If it helped, can you accept it?
Bas Helfrich
Bas Helfrich el 12 de En. de 2018
I am able to put in the a values, but not yet the x to be able to make a plot of the function.
To replace a value with a symbolic variable, use subs command. For instance
x=subs(x,1);
or
x=subs(x,0:0.1:1);
Bas Helfrich
Bas Helfrich el 12 de En. de 2018
And it works, thanks a lot!

Iniciar sesión para comentar.

Más respuestas (2)

Steven Lord
Steven Lord el 11 de En. de 2018

0 votos

Do you want this to be a symbolic expression with x as the symbolic variable? Or do you have a numeric value for x and want the result of evaluating this polynomial?
In the former case, see the poly2sym function.
In the latter case, I would be very careful about doing anything with a polynomial of degree 99. You're almost begging for catastrophic cancellation. But if you are careful you could use the polyval function.

2 comentarios

Bas Helfrich
Bas Helfrich el 12 de En. de 2018
Eventually I want to make a plot with this function with -8 < x < 8. So with numeric values for x. I'll try the polyval function, but I still have to define the function first...
Keep in mind:
>> x = 8^99
x =
2.5463e+89
You're going to be adding and subtracting extremely large numbers at the edges of your ranges if you go up to a degree 99 polynomial. See the catastrophic cancellation link I included in my response.
If your polynomial is of modest degree, you already have the polynomial you need to use polyval. It takes a coefficient vector and you said "I have the values for ak [the coefficients] in a vector".
x = polyval([1 2 3], 4)
y = 1*4^2 + 2*4^1 + 3*4^0
f = @(x) x.^2 + 2*x.^1 + 3;
z = f(4)

Iniciar sesión para comentar.

Torsten
Torsten el 12 de En. de 2018
Is this the partial Taylor series of a certain function f ?
In this case, you could use
f=...;
n=...;
fn = taylor(f,x,'order',n);
fplot(fn);
xlim([-4 4])
grid on
Best wishes
Torsten.

Etiquetas

Preguntada:

el 11 de En. de 2018

Comentada:

el 12 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by