首页 > 解决方案 > 如何在此代码上实现多个子间隔?

问题描述

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

[数学理论]- 众所周知,如果将区间分解为更小的区间,梯形规则会给出更准确的近似值,因此: I1 = [a; b1], I2 = [b1; b2], I3 = [b2; b3],...,I n-1 = [b n-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 

请帮忙。这将不胜感激!

标签: matlabmathintegrationintervalseconomics

解决方案


推荐阅读