MATLAB Answers

# I keep getting the error "Dimension argument must be a positive integer scalar within indexing range"

107 views (last 30 days)
Mostafa Moradi on 28 Feb 2021
Answered: Rik on 28 Feb 2021
I try to use the trapz to calculate the integral of function below from 0 to 30 but it keeps showing the error:
"Dimension argument must be a positive integer scalar within indexing range"
function: 200*(x/(7+x))*exp(-2.7*x/30)
Here is the code I wrote:
a=0;
b=30;
n=100;
h=(b-a)/n;
x= a:h:b;
f= @(x) 200*(x/(7+x))*exp(-2.7*x/b);
M= trapz(x,f)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Rik on 28 Feb 2021
The trapz function does not allow an anonymous function as second input. You will need to calculate the y values.
f= @(x) 200*(x./(7+x)).*exp(-2.7*x/b);
M= trapz(x,f(x))
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!