Matlab 如何在此代码上实现多个子间隔?

Matlab 如何在此代码上实现多个子间隔?,matlab,math,integration,intervals,economics,Matlab,Math,Integration,Intervals,Economics,我试图在下面给出的数学假设下优化函数(它基本上将代码中的当前间隔分解为多个子间隔,但我如何实现它?) [数学理论]-众所周知,如果将区间分解为更小的区间,则梯形规则给出了更精确的近似值:I1=[a;b1],I2=[b1;b2],I3=[b2;b3],…,In-1=[bn-1,bn],其中bn=b。使用上面的NC.m代码编写一个实现此策略的程序。它应该能够以任意n完成任务。必须创建多少子区间才能在区间[-3:0]上获得下面列出的函数的“精确”积分近似值 %For this problem wri

我试图在下面给出的数学假设下优化函数(它基本上将代码中的当前间隔分解为多个子间隔,但我如何实现它?)

[数学理论]-众所周知,如果将区间分解为更小的区间,则梯形规则给出了更精确的近似值:I1=[a;b1],I2=[b1;b2],I3=[b2;b3],…,In-1=[bn-1,bn],其中bn=b。使用上面的NC.m代码编写一个实现此策略的程序。它应该能够以任意n完成任务。必须创建多少子区间才能在区间[-3:0]上获得下面列出的函数的“精确”积分近似值


%For this problem write a script file called NC.m that implements 
%the Newton-Cotes method of integration for an arbitrary function f(x). It
%should takes as inputs the function and the limits of integration [a: b] and
%output the value of the definite integral. Specifically, you should use the
%Trapezoid rule as presented in Equation (11.73)

function [f]= NC(a,b,fun) %newton-cotes 

%a and b are limits of integration 

%setting it up 
fa= fun(a); %y value for lower limit 
fb= fun(b); %y value for upper limit 

%the actual function 
f= (b-a)*(fa+fb)/2; 

end 

%Result from estimation 
%fun= @(x) normpdf(x)  
%[f]= NC(-3,0,fun)-  0.6051 

%not accurate when compared to results from actual calculation  
%syms x 
%f= normpdf(x); 

%a= -3;- lower limit 
%b= 0;- higher limit 

%int(f, a, b)- 0.4897 


请帮忙。非常感谢

你能把文字写成文字,而不是贴一个截图的链接吗?当然!我会的@斯特夫刚刚做到了!非常感谢你!因此,代码的问题是,您不知道如何将段[a,b]分解为n个子段[a,b1],[b1,b2],[b2,b3],…,[b_(n-1),b]?您知道如何生成包含所有值[a,b1,b2,b3,…,b_(n-1),b]的数组吗?