定积分的scilab程序
BASEDIR='F:\scilabcode\'; exec(BASEDIR+'curseur_aire.sci', -1) // exec(BASEDIR+'newdata_aire.sci', -1) // exec(BASEDIR+'update_aire.sci', -1) curseur_aire()
function curseur_aire() global lock slider valueDisplay minDisplay maxDisplay line aedit bedit fcedit; global ff fig; global sliderMin sliderMax sliderSteps; try close(fig) end try delete(ff); end figWidth=600; // 图像宽度 figHeight=600; // 图像高度 fig=figure("Position",[10,10,figWidth,figHeight]); set(fig,"figure_name","面积计算求积分近似值"); fig.auto_resize = "off"; ff=scf(fig); ff.background = -2; axes=gca(); axes.box = "on"; axes.visible = "on"; axes.axes_bounds = [-0.05 -0.05 1.1 0.8]; axes.margins = [0.1 0.1 0.1 0.1]; // 框 frX=80; // 框的横坐标 frWidth=300; // 框的宽度 uicontrol(fig,"Style","frame",.. "Position",[frX,75,frWidth,100],.. "BackgroundColor",[0.9,0.9,0.9]); // 函数 fctWidth=100; uicontrol(fig,"Style","text",.. "Position" ,[frX+20,150,fctWidth,20],.. "BackgroundColor",[0.9,0.9,0.9],.. "Horizontalalignment","center",.. "String","输入函数:","FontSize",14,"FontWeight","bold"); fcWidth=150; fcedit=uicontrol(fig,"Style","edit",.. "Position",[frX+110,150,fcWidth,20],.. "String","x^2","FontSize",14,"FontWeight","bold"); // 端点 a atWidth=10; // 端点 a 标签宽度 uicontrol(fig,"Style","text",.. "Position" ,[(figWidth-atWidth)/2-190,120,atWidth,20],.. "BackgroundColor",[0.9,0.9,0.9],.. "Horizontalalignment","center",.. "String","a :","FontSize",14,"FontWeight","bold"); aWidth=40; // 端点 a 文本框宽度 aedit=uicontrol(fig,"Style","edit",.. "Position",[(figWidth-aWidth)/2-150,120,aWidth,20],.. "String","0","FontSize",14,"FontWeight","bold"); // 端点 b btWidth=10; // 端点 b 标签宽度 uicontrol(fig,"Style","text",.. "Position" ,[(figWidth-btWidth)/2-90,120,btWidth,20],.. "BackgroundColor",[0.9,0.9,0.9],.. "Horizontalalignment","center",.. "String","b :","FontSize",14,"FontWeight","bold"); bWidth=40; // 端点 b 标签宽度 bedit=uicontrol(fig,"Style","edit",.. "Position",[(figWidth-bWidth)/2-40,120,bWidth,20],.. "String","3","FontSize",14,"FontWeight","bold"); // 按钮 ok npokWidth=40; uicontrol(fig,"Style","pushbutton",.. "Position",[(figWidth-npokWidth)/2-80,90,npokWidth,20],.. "String","Ok","FontSize",14,"FontWeight","bold",.. "Callback","newdata_aire"); sliderMin=1; //滑条最小值 sliderMax=600; //滑条最大值 sliderX=50; // 滑条框的横坐标 sliderY=50; // 滑条框的纵坐标 sliderWidth=500; // 滑条框的长度 sliderHeight=20; // 滑条框的高度 sliderSteps=sliderMax-sliderMin; // 滑条移动的步数 //sliderSteps=100; // 滑条移动的步数 // 建立滑条 slider=uicontrol(fig,"Style","slider",.. "Position",[sliderX,sliderY,sliderWidth,sliderHeight],.. "Min",0,"Max",sliderSteps,.. "callback","update_aire"); // 显示端点最小值 lowerBoundPos =[sliderX-30,sliderY-10,20,sliderHeight]; uicontrol(fig,"Style","text",.. "Horizontalalignment","right",.. "Position",lowerBoundPos,.. "BackgroundColor", [1 1 1],.. "String",string(sliderMin),"FontSize",14,"FontWeight","bold"); // 显示端点最大值 upperBoundPos=[sliderX+sliderWidth+20,sliderY-10,30,sliderHeight]; uicontrol(fig,"style","text",.. "Horizontalalignment","left",.. "Position",upperBoundPos,.. "BackgroundColor", [1 1 1],.. "String",string(sliderMax),"FontSize",14,"FontWeight","bold"); // 显示当前值 valueDisplayPos=[sliderX+sliderWidth/2-50,sliderY-30,150,20] ; valueDisplay=uicontrol(fig,"style","text","Horizontalalignment","left",.. "BackgroundColor", [1 1 1],.. "Position",valueDisplayPos,"FontSize",14,"FontWeight","bold"); // 显示值 titlePos=[frX+frWidth+20,150,200,20]; maxDisplay=uicontrol(fig,"style","text","Horizontalalignment","left",.. "BackgroundColor", [1 1 1],.. "Position",titlePos,"FontSize",14,"FontWeight","bold"); titlePos=[frX+frWidth+20,120,200,20]; minDisplay=uicontrol(fig,"style","text","Horizontalalignment","left",.. "BackgroundColor", [1 1 1],.. "Position",titlePos,"FontSize",14,"FontWeight","bold"); lock=%t; endfunction
function newdata_aire() global a b N func hrmin hrmax croit ferreur; global lock line aedit bedit fcedit; global ff; ferreur=%F; fc=get(fcedit,"String"); try deff("[r]=func(x)","r="+fc); catch messagebox("函数定义时的语法错误!","Error","error","modal"); ferreur=%T; return; end a=get(aedit,"String"); [a,ierr]=evstr(a); if ierr<>0 then messagebox("端点a必须是数字","Error","error","modal"); ferreur=%T; return; end if type(a)<>1|size(a,"*")<>1 then messagebox("端点a必须是数字","Error","error","modal"); ferreur=%T; return; end b=get(bedit,"String"); [b,ierr]=evstr(b); if ierr<>0 then messagebox("端点b必须是数字","Error","error","modal"); ferreur=%T; return; end if type(b)<>1|size(b,"*")<>1 then messagebox("端点b必须是数字","Error","error","modal"); ferreur=%T; return; end if a >= b then ferreur=%T; messagebox("b必须比a大!","Error","error","modal"); return; end; //测试函数:考虑非向量语法 n=1000; xx=linspace(a,b,n); try for k=1:n yy(k)=func(xx(k)); end; if ~isreal(yy) then messagebox("函数计算过程中产生复数!","Error","error","modal"); ferreur=%T; return end catch [str,ierr]=lasterror(); select ierr case 27 then messagebox("函数计算过程中出现分母为0的情况","Error","error","modal"); case 32 then messagebox("在计算ln或者tan的过程中出现奇异值","Error","error","modal"); else messagebox("函数计算过程中出现错误","Error","error","modal"); end ferreur=%T; return end if min(yy) < 0 then messagebox("函数必须要大于0","Error","error","modal"); fError=%T; return; end; croit=%F; decroit=%F; for k=2:n if yy(k) >= yy(k-1) then croit=%T; else decroit=%T; end; end if croit & decroit then messagebox("函数必须是单调的","Error","error","modal"); ferreur=%T; return; end; //绘图 drawlater(); scf(ff); axes=gca(); if ~isempty(axes.children) then delete(axes.children); end // 当去处Inf与Nan后的新边界 infxx=find(isinf(xx)==%T); xx(infxx)=[]; yy(infxx)=[]; nanxx=find(isnan(xx)==%T); xx(nanxx)=[]; yy(nanxx)=[]; infyy=find(isinf(yy)==%T); xx(infyy)=[]; yy(infyy)=[]; nanyy=find(isnan(yy)==%T); xx(nanyy)=[]; yy(nanyy)=[]; axes.data_bounds=[min(xx),min(yy);max(xx),max(yy)]; plot(xx,yy); drawnow(); lock=%f; hrmin=[]; update_aire(); endfunction
function update_aire() global a b N hrmin hrmax croit ferreur; global lock slider valueDisplay minDisplay maxDisplay line hr; global sliderMin sliderMax sliderSteps; // 当加锁时返回 if lock then return end; // 当输入的数据存在错误时返回 if ferreur then return end; // 加锁 lock = %t ; drawlater(); if hrmin <> [] then delete(hrmin); delete(hrmax); end // 显示当前N的值 curVal=get(slider,"Value"); N=curVal*(sliderMax-sliderMin)/sliderSteps+1; N=floor(N); set(valueDisplay,"String","N的值: "+string(N)); // delimitation of the right and display the error [s,S]=approche(a,b,N); if croit then //输入函数递增 set(maxDisplay,"String","最大值: "+string(S)); set(minDisplay,"String","最小值: "+string(s)); else //输入函数递减 set(maxDisplay,"String","最大值: "+string(s)); set(minDisplay,"String","最小值: "+string(S)); end drawnow(); // 解锁 lock=%f; endfunction function [s,S]=approche(a,b,n) global func hrmin hrmax croit; s=0; for k=0:n-1; s=s+func(a+k*(b-a)/n) end s=s*(b-a)/n; S=0; for k=1:n x(k)=a+(k-1)*(b-a)/n; y(k)=func(x(k)); S=S+func(a+k*(b-a)/n) end x(n+1)=b; y(n+1)=func(x(n+1)); S=S*(b-a)/n; Xmin=[];Ymin=[]; Xmax=[]; Ymax=[]; if croit then for i=1:n Xmin=[Xmin;x(i);x(i);x(i);x(i+1)]; Ymin=[Ymin;0;y(i);y(i);y(i)]; Xmax=[Xmax;x(i);x(i);x(i);x(i+1)]; Ymax=[Ymax;y(i);y(i+1);y(i+1);y(i+1)]; end Xmin=[Xmin;x(n+1);x(n+1)]; Ymin=[Ymin;y(n);0]; Xmax=[Xmax;x(n+1);x(n+1)]; Ymax=[Ymax;y(n+1);y(n)]; else for j=1:n i=n+2-j; Xmin=[Xmin;x(i);x(i);x(i);x(i-1)]; Ymin=[Ymin;0;y(i);y(i);y(i)]; Xmax=[Xmax;x(i);x(i);x(i);x(i-1)]; Ymax=[Ymax;y(i);y(i-1);y(i-1);y(i-1)]; end Xmin=[Xmin;x(1);x(1)]; Ymin=[Ymin;y(2);0]; Xmax=[Xmax;x(1);x(1)]; Ymax=[Ymax;y(1);y(2)]; end //绘制折线(矩形) xpoly(Xmin,Ymin); hrmin=gce(); hrmin.foreground=color("green"); xpoly(Xmax,Ymax); hrmax=gce(); hrmax.foreground=color("black"); endfunction