Can anyone convert this Pseudocode into Matlab?

FUNCTION Simp13m (h, n, f)
sum = f(0)
DOFOR i=1, n-2, 2
sum = sum + 4 * f_i + 2 * f_i+1
END DO
sum = sum + 4 * f_n-1 + f_n
Simp13m = h * sum / 3
END Simp13m

 Accepted Answer

Looks like Fortran (and assuming f is an array and f_i etc are indexing into the f array). So maybe something like this, with some variable names changed so they don't clash with MATLAB functions. Put this in a file with the name Simp13m.m
function result = Simp13m (h, n, f)
result = f(1); % <-- MATLAB array indexing starts at 1
for i=2:2:n-2 % <-- Bump up the starting index for this loop per comment above
result = result + 4 * f(i) + 2 * f(i+1);
end
result = result + 4 * f(n-1) + f(n);
result = h * result / 3;
end

More Answers (0)

Categories

Find more on Programming 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!