0

I have a function and I want to each time change its input and integrate over a certain interval. For example, I first write an m file:

function y = myfun(x) 

y = 1 ./ (x.^3 - 2*x - 5);

Now I want to integrate functions like myfun(x-2) or myfun(2*x). Does anyone know how I should pass them? Integral(myfun(x-2),a,b) creates an error.

Thanks

erikced
  • 712
  • 4
  • 13
user2225868
  • 33
  • 2
  • 7

1 Answers1

0

I suggest calling integral on a handle for your function as below:

h1 = @(x)myfun(x);
h2 = @(x)myfun(x-2);
h3 = @(x)myfun(x.^2);

integral(h1,a,b);
integral(h2,a,b);
integral(h3,a,b);

This should trick the integral function into thinking that you are just defining myfun as a function of x while allowing you to pass whatever expression you want to it. You could also pass additional parameters this way, such as:

h = @(x)myfun(x, params);
integral(h,a,b);

Where params could be a list of parameters that you use in your definition of myfun.

I hope that helps.

EDIT: I tested this on a server that I have access to which does have the integral function and it seemed to work. Hopefully this answers your question.

Engineero
  • 12,340
  • 5
  • 53
  • 75