Stephanie
Stephanie

Reputation: 15

Integral in Matlab

I have 3 equations:

f = (exp(-x.^2)).*(log(x)).^2 g = exp(-x.^2) h = (log(x)).^2

The interval is:

x = 0.05:10

I am able to correctly plot the equations but when I try to find an integral, it says that there is an error.

The code I used to find an integral is:

integral(f,0,Inf) integral(g,0,inf) integral(h,0,10)

The integrals for f and g are from 0 to infinity and the integral for h is from 0 to 10. None of my code to find integrals works.

Upvotes: 1

Views: 79

Answers (1)

srknzl
srknzl

Reputation: 787

You need to define f,g,h as functions like shown below. See documentation of integral(), it takes a function as its first argument. Matlab integral documentation

x = 0.05:10
f = @(x) (exp(-x.^2)).*(log(x)).^2
g = @(x) exp(-x.^2)
h = @(x) (log(x)).^2
integral(f,0,Inf)  % 1.9475
integral(g,0,inf) % 0.8862
integral(h,0,10) % 26.9673
h = @(x) (log(x)).^2

This syntax is called anonymous functions, basically they are nameless functions. In above case it takes x as input and returns log(x) squared. From now on h is a function and it can be used like this.

h(1)  % will be equal 0

For more on anonymous functions refer to matlab anonymous functions guide: Anonymous Functions

Upvotes: 1

Related Questions