I am trying to pass a function from another .m file into a quadl call that also takes an extra variable when evaluating the function.
My current call looks like:
fun=@fun1
min = 0;
max = 2;
y=quadl(fun, min, max, 0.00001);
I want to have fun1 to be evaluated from min to max.
My function in the fun1.m file is:
function func=fun1(x)
func = x^2+x+y
How do I pass the 'y' variable in?
I have tried to change the quad call to:
y=quadl(fun1(y), min, max, 0.00001);
and:
y=quadl(fun1(y), min, max, 0.00001);
and the function to:
function func=fun1(x,y)
func = x^2+x+y
but that does not work.
I have also tried declaring a global variable but it is giving me an undeclared variable error.
Help is appreciated!
First of all quadl
will be removed in future releases, so it's best to use integral
.
You should use:
function func=fun1(x,y)
func = x.^2+x+y; % with .^
Then in your other script, you declare fun
as a function of x
:
y = 5;
fun=@(x)fun1(x,y);
min = 0;
max = 2;
y=integral(fun, min, max);
If you insist on using quadl
, you can do exactly the same:
y = 5;
fun=@(x)fun1(x,y);
min = 0;
max = 2;
y=quadl(fun, min, max, 0.00001);
EDIT:
To keep an expression in function of y
aft er the integration of x
, you need to work with syms
and int
:
syms x y z
fun = fun1(x,y,z);
min = 0;
max = 2;
expr = int(fun, x, min, max);