MATLAB Answers

0

How to implement such function?

Asked by Eray Tunç YILMAZ on 23 May 2019
Latest activity Answered by 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?

  0 Comments

Sign in to comment.

1 Answer

Answer by 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.

  0 Comments

Sign in to comment.