User defined function with vector inputs returns incorrect values?
Show older comments
Hello
I wrote a user-defined function m-file to use the Trapezoidal Rule in order to estimate the integral of a function over a certain interval [a b]. The inputs will include the function (as an anonymous function handle), the interval, and the number of segments to be divided. Output is the integral estimate. Here is the m-file so far:
function [I] = trapezoid(fun,Int,n)
% trapezoid: Implements the trapezoidal rule on a function over a
% specified interval for n number of segments. n can be a scalar or a
% vector.
%input:
%fun = function to be integrated
%Int = [a b] = integration limits
%n = number of segments
%output:
%I = integral estimate
a = Int(:,1);
b = Int(:,2);
I = zeros(1,length(n)); %preallocate solution vector
if length(n) > 1
for i = 1:length(n)
I(i) = trapezoid(fun,Int,n(i));
end
end
x = a;
h = (b - a)./n;
s = fun(a);
en = n-1;
for j = 1:en
x = x + h;
s = s + 2.*fun(x);
end
s = s + fun(b);
I = s.*(h./2);
So far, when using the example anonymous function from the command window
fun = @(x) 4*x.^3 - 42*x.^2 + 300
over the interval [0 10], when a scalar value is used for n, the function m-file works and gives verifyable answers. But when the number of segments input is a vector, such as n=[2 3 4 5 10 15 20 30 40 50], the first value of the solution vector is correct, but the others are incorrect (the solution vector that outputs from n=[2 3 4 5 10 15 20 30 40 50] is
[-250.0000 604.9383 750.0000 728.0000 462.0000 321.6790 245.0000 165.1605 124.3594 99.6704]
, when the correct values should be
[-250, -666.6667, -812.5000, -880, -970, -986.6667, -992.5000, -996.6667, -998.1250, -998.8000]
for the trapezoidal rule).
How can I get my program m-file to accept the vector for the number of segments input AND return the correct results? I have tried vectorizing & adding "." before multiplication/division operators. How can I get the correct output values? Thank you
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!