How to implement such function?

1 view (last 30 days)
Eray Tunç YILMAZ
Eray Tunç YILMAZ on 23 May 2019
Answered: James Tursa on 23 May 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?

Answers (1)

James Tursa
James Tursa on 23 May 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.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!