How to implement such function?

11 vues (au cours des 30 derniers jours)
Eray Tunç YILMAZ
Eray Tunç YILMAZ le 23 Mai 2019
I am trying to write a script to implemet a function such as this one
function[output1, output2, ....] = my_function(constant, coefficient_vector, xValue)
where a0=contant, a1=coefficient_vector(1), a2=coefficient_vector(2), ... and so on.
coefficient_vector may vary in size and content i.e it could be [ 1 ] or it could be [ 1 2 1 3 1 ] or [ 0 7 5 0 6 4 .......] positive integers only, without any size restriction. Also, constant could be any integer.
In case of [ 1 ], script should print out f(x) = cos(x). In case of [ 1 1 1 1 1 ], print out f(x) = cos(x) + 2*cos(2*x) + cos(3*x) + 3*cos(4*x) + cos(5*x) and so on...
Then calculate the value of f(xValue)
I have already did it as using symbolic function but the task is to do without symbolics
syms f(x);
f(x)=0;
for i=1:n
f(x) = coeffVector(i) * cos(i*x) + f(x);
end
f(x) = f(x) + constant;
12.PNG
So, how can I do this?

Réponses (1)

James Tursa
James Tursa le 23 Mai 2019
Basically, just replace f(x) with f. E.g.,
f = constant;
:
f = coeffVector(i) * cos(i*x) + f;
You could also do this without a loop.

Community Treasure Hunt

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

Start Hunting!

Translated by