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

 채택된 답변

James Tursa
James Tursa 2016년 5월 16일

0 개 추천

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에 대해 자세히 알아보기

질문:

2016년 5월 16일

답변:

2016년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by