MATLAB: How to pass arguments into a function handle that is then used in fsolve

fsolve

I'm trying to solve a transcendental equation for the variable 'neff'. I have a function in a file called 'transcendental' containing this function, which I then call in the main code and pass into fsolve. I need to be able to pass in the value of 'w', however. The code works when 'w' is inside the function, but I can't find a way to be able to pass 'w' into the function.
I'm new to matlab and have really tried to fix this for hours with anonymous functions, nested functions, you name it. I would be extremely grateful for any help!
function saved in 'transcendental.m':
function[fun] = F(neff,w)
w = 1.26*10e15
c = 2.998e8
ns = 1.5
nc = 1.0
d = 2e-6
beta = (w/c)*neff
k = sqrt(((w^2)/(c^2))*(ns)^2 - beta^2)
g = sqrt(beta^2 - (nc)^2*((w^2)/(c^2)))
fun = tan(k*d)+k/g
main code:
fun = @transcendental
x0 = [1.2]
answer = fsolve(@transcendental, x0)

Best Answer

function out = transcendental(neff,w) % best to use same name as the file.
% Do NOT define |w| here!
c = 2.998e8;
ns = 1.5;
nc = 1.0;
d = 2e-6;
beta = (w/c)*neff;
k = sqrt(((w^2)/(c^2))*(ns)^2 - beta^2);
g = sqrt(beta^2 - (nc)^2*((w^2)/(c^2)));
out = tan(k*d)+k/g;
end
And then call it like this:
>> w = 1.26e15;
>> x0 = 1.2;
>> val = fsolve(@(x)transcendental(x,w), x0)
val = 1.3416
For more information see: