Can integrals be solved using MATLAB's ode solver?

1k Views Asked by At

I was wondering how I could solve a definite integral using a MATLAB ode solver? Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Sure, you can do it, but Matlab also has an integral function that may be simpler to apply. Here is an example for $$\int_0^1\frac{dx}{1+x^2}=\frac{\pi}4$$

% test.m

f = @(t,x) 1./(1+t.^2); % Define integrand
a = 0; % Define limits
b = 1;
y0 = 0; % Initial value is zero
[t,y] = ode45(f,[a b],y0);
format long;
y(end)
fun = @(x) 1./(1+x.^2);
integral(fun,a,b)
pi/4
format;

Output:

test

ans =

   0.785398163863257


ans =

   0.785398163397448


ans =

   0.785398163397448