Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.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)_Matlab_Integration - Fatal编程技术网

如何正确使用积分极限(Matlab)

如何正确使用积分极限(Matlab),matlab,integration,Matlab,Integration,我制作了一个脚本,可以计算一个给定的带极限的积分: 0 <= x <= 2 and 0 <= y <= 1 这是我针对早期限制的脚本: L = 100; M = L/2; hx = (2)/L; hy = (1)/M; x=[0:L]*hx; y=[0:M]*hy; Fx=[]; for i = 1:L+1 Fy=[]; for j = 1:M+1 f = inte(x(i),y(j)); Fy = [F

我制作了一个脚本,可以计算一个给定的带极限的积分:

0 <= x <= 2 and 0 <= y <= 1 
这是我针对早期限制的脚本:

L = 100; M = L/2;

hx = (2)/L; hy = (1)/M; 

x=[0:L]*hx;
y=[0:M]*hy;

Fx=[];

for i = 1:L+1

    Fy=[];

    for j = 1:M+1

        f = inte(x(i),y(j));
        Fy = [Fy f];

    end

    ycor = hy*(sum(Fy) - Fy(1)/2 - Fy(end)/2);
    Fx = [Fx ycor];

end

ans = hx*(sum(Fx) - Fx(1)/2 - Fx(end)/2);

disp (ans)
当我试图更改代码时,似乎找不到正确的答案。正确答案应该是0.1560949

L是x方向上的步数,M是y方向上的步数。hx和hy是步长。 这真让我烦透了。不,我只能使用integral2或traps命令作为参考


提前谢谢

在您当前的代码中

hy = (1)/M; 
y=[0:M]*hy;
请参阅y变量。当y的限制取决于x时,这些线不能停留在x上的循环之外:它们应该移入并使用值x(i)。像这样:

for i = 1:L+1   % as in your code

   hy = (sin(pi*x(i)/2))/M;     
   y = [0:M]*hy;

   Fy=[];  % this and the rest as in your code 

我得到了您想要的输出
0.1561

您已经发布了原始第一个案例的脚本。在第二种情况下你尝试了什么,其中y是x的函数?
hy = (1)/M; 
y=[0:M]*hy;
for i = 1:L+1   % as in your code

   hy = (sin(pi*x(i)/2))/M;     
   y = [0:M]*hy;

   Fy=[];  % this and the rest as in your code