Matlab: variable-dependent dot product in anonymous function

206 Views Asked by At

I have an anonymous function like this one:

f = @(t) 5.*cos(t) + 10.*sin(t);
f([1,2])

ans =

   11.1162    7.0122

Is it possible to define f using a dot product operation or similar, something along the lines of this?

f = @(t) dot([5,10], [cos(t), sin(t)]);

(which obviously fails when t has more than 1 element)

My actual problem involves much larger vectors, where typing it all out doesn't look too neat.

2

There are 2 best solutions below

0
On

Consider doing dot by hand:

sum((ones(size(t,1),1)*[5,10]) .*[cos(t),sin(t)],2);

I assumed t is an n x 1 vector.

0
On

Note that for column vectors of the same size, $u \cdot v = u^Tv$. For row vectors we would have $u \cdot v = uv^T$.

Put simply, if your coefficient vector is a row vector, just write

C = [5 10];
f = @(t)C*[cos(t) sin(t)]';

Make sure t is of the appropriate dimensionality, of course.