Question

I have a function handle in matlab that I want to input a vector to return a vector that went through the function mapping. But this function varies in two parameters so I want to be able to input a value for that scalar as well as the function values.

The function is:

syms a
g = @(y) (exp(((-1/a).*atanh(y)).^2).*cos((2/a).*atanh(y)).*(1/a).*(1/(1+y.^2)));

or I can also do

g = @(y,a) (exp(((-1/a).*atanh(y)).^2).*cos((2/a).*atanh(y)).*(1/a).*(1/(1+y.^2)));

What I want to be able to do is

values = g([-0.5 0 0.5],3);

and return a three element vector.

but this returns an error and I'm not sure how I can get it to perform as I want.

Was it helpful?

Solution

I think you forgot one . with the last division:

g = @(y,a) (exp(((-1/a).*atanh(y)).^2).*cos((2/a).*atanh(y)).*(1/a).*(1./(1+y.^2)));

this works for me.

BTW: If you do it like this:

g = @(y,a) (exp(((-1./a)*atanh(y)).^2).*(cos((2./a)*atanh(y))).*((1./a)*(1./(1+y.^2))));

You can input both y and a as vectors:

g([-0.5 0 0.5],[1 2 3]')
ans =
   0.49202   1.00000   0.49202
   0.36789   0.50000   0.36789
   0.25747   0.33333   0.25747

this reads:

g([a b c], [d e f]')
ans = 
   g(a,d)    g(b,d)    g(c,d)
   g(a,e)    g(b,e)    g(c,e)
   g(a,f)    g(b,f)    g(c,f)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top