How to define a function with a summation

17 visualizaciones (últimos 30 días)
Rozalia Korycka
Rozalia Korycka el 30 de Mayo de 2019
Comentada: Sahil el 18 de Sept. de 2022
Hello,
I'm fairly new to matlab so I'm not too familiar with the way all of the functions work. I need to plot a function I have been given :
but I am having trouble defining it. I've tried the following:
syms k x
f = matlabFunction(b0 + symsum(a(k)*sin(k*x) + b(k)*cos(k*x)));
This gives me an error stating "Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression."
Can someone help me out? :)
  1 comentario
Sahil
Sahil el 18 de Sept. de 2022
syms k x f = matlabFunction(b0 + symsum(a(k)*sin(k*x) + b(k)*cos(k*x)));

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 30 de Mayo de 2019
Editada: Matt J el 30 de Mayo de 2019
An efficient implementation would be,
a=___;
b=___;
b0=___;
fplot(@(z) myFourier(z,a,b,b0));
function f=myFourier(x,a,b,b0)
n=numel(a);
arg=x(:).*(1:n);
f=b0+sin(arg)*a(:)+cos(arg)*b(:);
f=reshape(f,size(x));
end
  3 comentarios
Matt J
Matt J el 30 de Mayo de 2019
I'm baffled by the warnings. The function is vectorized as fplot requires.
Rozalia Korycka
Rozalia Korycka el 30 de Mayo de 2019
That's exactly the opposite of what the warning says hahah but I don't know since I don't even know what is that supposed to mean

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by