3
J'ai écrit une fonction pour calculer l'intégrale en utilisant la règle de quadrature gaussienne. J'ai besoin de vectoriser les 2 dernières lignes, sans utiliser de boucle. Des idées?vectorisation Matlab - comment vectoriser la fonction de Gauss (code) sans boucle
function Q = gauss5(f,a,b,n)
% Divide the interval into n parts
y = linspace(a,b,n);
% Create function to use arrayfun
% Sum up the result from the arrayfun with function Fun on particular
% intervals, the first argument is array y without the last value, and
% the second argument is the array y without the first value
a = (y(1:end-1));
b = (y(2:end));
% coefficients - some random numbers
c = [1 2 3 4 5];
% nodes - random numbers too
x = [1 1 1 1 1];
% calculate b-a
w = b-a;
% sum up the result
Q = 0;
for i=1:n-1
Q = Q + sum((w(i)*c.*feval(f,((w(i)*x)+a(i)))));
end
end
est-f peut être différentes fonctions? Et est-ce que n est toujours inférieur à la longueur de a et de b? –