Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何用for循环的答案填充零向量?_Matlab_Octave - Fatal编程技术网

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