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

 Respuesta aceptada

James Tursa
James Tursa el 16 de Mayo de 2016
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

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Mayo de 2016

Respondida:

el 16 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by