matlab creating n trigonometric functions

32 Views Asked by At

I have to make a program that as input gets the vectors t and y, and an order n, and I then have to make n functions that i have to use to make a least square fit. But my problem lies in making the n functions - they have to have the form:

$f_1(t)=1$, $f_2(t)=sin(wt)$, $f_3(t)=cos(wt)$, $f_4(t)=sin(2wt)$, $f_5(t)=cos(2wt)$

and so on until $f_n$.I have been trying to do this with a lot of for loops but I can't really get it to work - I was wondering if some one had an easy way to do it - I am trying to get the functions into a cell array, so that I can save the functions as functions of t :)

1

There are 1 best solutions below

0
On BEST ANSWER

Something like this should work for you:

omega = 1;
C = cell(n, 1);
f = @(t) ones(size(t));
C{1} = f;
for i = 2:n
    if (mod(i,2) == 0)
        f = @(t) sin(0.5 * i * omega * t);
    else
        f = @(t) cos(0.5 * (i - 1) * omega * t);
    end
    C{i} = f;
end