0

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.

  • Please use MathJax: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference – JohnWO Oct 23 '14 at 09:20
  • @JohnWO: The tutorial says much about typesetting formulas, nothing about code excerpts. What should I have done differently? – Jenny542 Oct 23 '14 at 09:58

2 Answers2

1

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.

Steffen
  • 236
0

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.

Emily
  • 35,688
  • 6
  • 93
  • 141