Matlab 如何用for循环的答案填充零向量?
我创建了一个名为Matlab 如何用for循环的答案填充零向量?,matlab,octave,Matlab,Octave,我创建了一个名为 result = zeros(1,20); 在GNU倍频程GUI上,我想用for循环的积分估计来填充零 a = 0; b = 3; f = @(x) sin(10 *x.^2); for i = 20 n = 2.^i; dx = (b - a) / n; left = a:dx: (b - dx); right = (a+dx) :dx:b; h1 = f(left); h2 = f(right); areas =
result = zeros(1,20);
在GNU倍频程GUI上,我想用for循环的积分估计来填充零
a = 0;
b = 3;
f = @(x) sin(10 *x.^2);
for i = 20
n = 2.^i;
dx = (b - a) / n;
left = a:dx: (b - dx);
right = (a+dx) :dx:b;
h1 = f(left);
h2 = f(right);
areas = (h1 + h2) ./ 2 .* dx;
totalarea = sum(areas)
endfor
有谁能告诉我如何用上面for循环中的totalarea
值填充result
矩阵吗?请参见注释:
result = zeros(1,20);
a = 0;
b = 3;
f = @(x) sin(10 *x.^2);
for i = 1:20 % make sure you loop from 1 to 20
n = 2.^i;
dx = (b - a) / n;
left = a:dx: (b - dx);
right = (a+dx) :dx:b;
h1 = f(left);
h2 = f(right);
areas = (h1 + h2) ./ 2 .* dx;
totalarea = sum(areas);
result(i) = totalarea; % put the totalarea in the i'th position in the result matrix
endfor