Trapezoidal Numerical Integration in MATLAB without using a FOR loop? -
i'm following numerical methods course , made small matlab script compute integrals using trapezoidal method. script uses loop , friend told me i'm doing wrong if use loop in matlab. there way convert script matlab-friendly one?
%number of points use n = 4; %integration interval = 0; b = 0.5; %width of integration segments h = (b-a) / n; f = exp(a); = 1:n-1 f = f + 2*exp(a+i*h); end f = f + exp(b); f = h/2*f
vectorization important speed , clarity, using built-in functions whenever possible. matlab has built in function trapezoidal numerical integration called trapz. here example.
x = 0:.125:.5 y = exp(x) f = trapz(x,y)
Comments
Post a Comment