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

 采纳的回答

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

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by