matlab程序设计实例(用Matlab编写程序实现功能)

本文目录
- 用Matlab编写程序实现功能
- matlab的程序设计 一个三位整数各位数字的立方和等于该数本身则称该数为水仙花数输出全部水仙花数
- MATLAB程序设计
- matlab程序设计
- 用MATLAB设计!
- matlab实用例子大全、求!!!
- 急需一道matlab程序设计题,至少30行,能运行出来的,另外能解释一下这个程序是干什么的,谢谢了
用Matlab编写程序实现功能
function = drawn( n )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if nargin==0%没有输入参数
n=1000;%用1000边形模拟圆
textout=’圆’;
elseif n》2 && n==fix(n)%输入n
textout=;
else
error(’请输入大于2的自然数’);%输入出错
end
d=linspace(0,360,n+1);%360度分n分
x=cosd(d);%计算x y
y=sind(d);
plot(x,y);%画图
title(textout);
axis square;
figure(gcf);
end
matlab的程序设计 一个三位整数各位数字的立方和等于该数本身则称该数为水仙花数输出全部水仙花数
for i=100:999
j=fix(i/100);
k=fix((i-100*j)/10);
t=i-100*j-10*k;
if j*j*j+k*k*k+t*t*t==i
i
end
end
fix是取整函数,这样写可以,但答案不唯一。望采纳!
MATLAB程序设计
peaks是MATLAB自带的双变量的实例函数。使用格式:
Z = peaks %生成49×49的矩阵
Z = peaks(n) %生成n×n的矩阵
Z = peaks(X,Y) %计算给定x和y(必须是相同大小)的峰值,并返回一个大小相同的矩阵。
其他应用,如
》》 = peaks(10)
》》 surf(X,Y,Z)
matlab程序设计
function shili29
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例29’);
=meshgrid(-2:0.1:2,... %网格绘制
-2:0.1:2,...
-2:0.1:2);
v=x.*exp(-x.^2-y.^2-z.^2); %.^, .*表示向量操作。
grid on
for i=-2:0.5:2;
h1=surf(linspace(-2,2,20),... %linespace就是空间线性分布
linspace(-2,2,20),...
zeros(20)+i);
rotate(h1,,30)
dx=get(h1,’xdata’);
dy=get(h1,’ydata’);
dz=get(h1,’zdata’);
delete(h1)
slice(x,y,z,v,,2,-2)
hold on
slice(x,y,z,v,dx,dy,dz)
hold off
axis tight
view(-5,10)
drawnow
end
function=integral(f,from,to); %函数声明
index =from:0.01:to;
m =length(index);
for n =1:m
outtp(n) =subs(f,index(n));
end
out=sum(outtp)*0.01;
syms x y
y =x^2;
int(det(y),x,-2,5);
syms x y z
a=;
int(int(int(det(a),x,-1,1),y,-1,1),z,-0.5,0.5) %int 是系统积分函数
function =find_matrix(a);
handle =fopen(’pos.txt’,’w’);
dim =size(a);
C =reshape(a,1,);
row =sort(C,’descend’);
fprintf(handle,’After sort : %g \r\n\r\n’,row);
for index =1:length(C)
pos = find(a==C(index));
if mod(pos,dim(1))==0
rowtp =dim(1);
else
rowtp =mod(pos,dim(1));
end
if pos/dim(1)-mod(pos/dim(1),1)》0
cltp =pos/dim(1)-mod(pos/dim(1),1);
else
cltp =1;
end
fprintf(handle,’%g is at : row = %g ,column =%g \r\n\r\n’,C(index),rowtp,cltp);
end
fclose(handle);
end
自己跑程序就行了,也可以在command line 输入》》 help fprintf 等以获得帮助。
用MATLAB设计!
1,使用MATLAB计算滤波器的H参数。
利用MATLAB提供的remez函数可算得滤波器的H参数。为了达到设计要求,参数选取时留有一定余地。这里选取αp=0.9dB,αs=31dB。取样频率则选择260MHz。
2,MATLAB程序设计
源程序如下:
TF=; %过度带频率
m=;
d=; %δp与δs
Fs=260000000; %采样频率为260MHz
= remezord(TF,m,d,Fs);
b = remez(N,fpts,mag,wt) %滤波器的h参数
= freqz(b,1,256);
plot(w/pi,20*log10(abs(h))); %分析其频谱
grid; %显示网格
matlab实用例子大全、求!!!
1-32是:图形应用篇
33-66是:界面设计篇
67-84是:图形处理篇
85-100是:数值分析篇
实例1:三角函数曲线(1)
function shili01
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例01’);
h1=axes(’parent’,h0,...
’visible’,’off’);
x=-pi:0.05:pi;
y=sin(x);
plot(x,y);
xlabel(’自变量X’);
ylabel(’函数值Y’);
title(’SIN( )函数曲线’);
grid on
实例2:三角函数曲线(2)
function shili02
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例02’);
x=-pi:0.05:pi;
y=sin(x)+cos(x);
plot(x,y,’-*r’,’linewidth’,1);
grid on
xlabel(’自变量X’);
ylabel(’函数值Y’);
title(’三角函数’);
实例3:图形的叠加
function shili03
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例03’);
x=-pi:0.05:pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,...
’-*r’,...
x,y2,...
’--og’);
grid on
xlabel(’自变量X’);
ylabel(’函数值Y’);
title(’三角函数’);
实例4:双y轴图形的绘制
function shili04
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例04’);
x=0:900;a=1000;b=0.005;
y1=2*x;
y2=cos(b*x);
=plotyy(x,y1,x,y2,’semilogy’,’plot’);
axes(haxes(1))
ylabel(’semilog plot’);
axes(haxes(2))
ylabel(’linear plot’);
实例5:单个轴窗口显示多个图形
function shili05
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例05’);
t=0:pi/10:2*pi;
=meshgrid(t);
subplot(2,2,1)
plot(sin(t),cos(t))
axis equal
subplot(2,2,2)
z=sin(x)-cos(y);
plot(t,z)
axis()
subplot(2,2,3)
h=sin(x)+cos(y);
plot(t,h)
axis()
subplot(2,2,4)
g=(sin(x).^2)-(cos(y).^2);
plot(t,g)
axis()
实例6:图形标注
function shili06
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例06’);
t=0:pi/10:2*pi;
h=plot(t,sin(t));
xlabel(’t=0到2\pi’,’fontsize’,16);
ylabel(’sin(t)’,’fontsize’,16);
title(’\it{从 0to2\pi 的正弦曲线}’,’fontsize’,16)
x=get(h,’xdata’);
y=get(h,’ydata’);
imin=find(min(y)==y);
imax=find(max(y)==y);
text(x(imin),y(imin),...
,...
’fontsize’,16)
text(x(imax),y(imax),...
,...
’fontsize’,16)
实例7:条形图形
function shili07
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例07’);
tiao1=;
tiao2=;
t=0:7;
bar(t,tiao1)
xlabel(’X轴’);
ylabel(’TIAO1值’);
h1=gca;
h3=axes(’position’,get(h1,’position’));
plot(t,tiao2,’linewidth’,3)
set(h3,’yaxislocation’,’right’,’color’,’none’,’xticklabel’,)
实例8:区域图形
function shili08
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例08’);
x=91:95;
profits1=;
profits2=;
profits3=;
profits4=;
area(x,profits1,’facecolor’,,...
’edgecolor’,’b’,...
’linewidth’,3)
hold on
area(x,profits2,’facecolor’,,...
’edgecolor’,’y’,...
’linewidth’,3)
hold on
area(x,profits3,’facecolor’,,...
’edgecolor’,’r’,...
’linewidth’,3)
hold on
area(x,profits4,’facecolor’,,...
’edgecolor’,’m’,...
’linewidth’,3)
hold off
set(gca,’xtick’,)
set(gca,’layer’,’top’)
gtext(’\leftarrow第一季度销量’)
gtext(’\leftarrow第二季度销量’)
gtext(’\leftarrow第三季度销量’)
gtext(’\leftarrow第四季度销量’)
xlabel(’年’,’fontsize’,16);
ylabel(’销售量’,’fontsize’,16);
实例9:饼图的绘制
function shili09
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例09’);
t=[54 21 35;
68 54 35;
45 25 12;
48 68 45;
68 54 69];
x=sum(t);
h=pie(x);
textobjs=findobj(h,’type’,’text’);
str1=get(textobjs,{’string’});
val1=get(textobjs,{’extent’});
oldext=cat(1,val1{:});
names={’商品一:’;’商品二:’;’商品三:’};
str2=strcat(names,str1);
set(textobjs,{’string’},str2)
val2=get(textobjs,{’extent’});
newext=cat(1,val2{:});
offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2;
pos=get(textobjs,{’position’});
textpos=cat(1,pos{:});
textpos(:,1)=textpos(:,1)+offset;
set(textobjs,{’position’},num2cell(textpos,))
实例10:阶梯图
function shili10
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例10’);
a=0.01;
b=0.5;
t=0:10;
f=exp(-a*t).*sin(b*t);
stairs(t,f)
hold on
plot(t,f,’:*’)
hold off
glabel=’函数e^{-(\alpha*t)}sin\beta*t的阶梯图’;
gtext(glabel,’fontsize’,16)
xlabel(’t=0:10’,’fontsize’,16)
axis()
实例11:枝干图
function shili11
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例11’);
x=0:pi/20:2*pi;
y1=sin(x);
y2=cos(x);
h1=stem(x,y1+y2);
hold on
h3=plot(x,y1,’^r’,x,y2,’*g’);
hold off
h3=;
legend(h3,’y1+y2’,’y1=sin(x)’,’y2=cos(x)’)
xlabel(’自变量X’);
ylabel(’函数值Y’);
title(’正弦函数与余弦函数的线性组合’);
实例12:罗盘图
function shili12
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例12’);
winddirection=[54 24 65 84
256 12 235 62
125 324 34 254];
windpower=[2 5 5 3
6 8 12 7
6 14 10 8];
rdirection=winddirection*pi/180;
=pol2cart(rdirection,windpower);
compass(x,y);
desc={’风向和风力’,
’北京气象台’,
’10月1日0:00到’,
’10月1日12:00’};
gtext(desc)
实例13:轮廓图
function shili13
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例13’);
=meshgrid((0:10:360)*pi/180,0:0.05:1);
=pol2cart(th,r);
z=x+i*y;
f=(z.^4-1).^(0.25);
contour(x,y,abs(f),20)
axis equal
xlabel(’实部’,’fontsize’,16);
ylabel(’虚部’,’fontsize’,16);
h=polar();
delete(h)
hold on
contour(x,y,abs(f),20)
实例14:交互式图形
function shili14
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例14’);
axis();
hold on
x=;
y=;
n=0;
disp(’单击鼠标左键点取需要的点’);
disp(’单击鼠标右键点取最后一个点’);
but=1;
while but==1
=ginput(1);
plot(xi,yi,’bo’)
n=n+1;
disp(’单击鼠标左键点取下一个点’);
x(n,1)=xi;
y(n,1)=yi;
end
t=1:n;
ts=1:0.1:n;
xs=spline(t,x,ts);
ys=spline(t,y,ts);
plot(xs,ys,’r-’);
hold off
实例14:交互式图形
function shili14
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例14’);
axis();
hold on
x=;
y=;
n=0;
disp(’单击鼠标左键点取需要的点’);
disp(’单击鼠标右键点取最后一个点’);
but=1;
while but==1
=ginput(1);
plot(xi,yi,’bo’)
n=n+1;
disp(’单击鼠标左键点取下一个点’);
x(n,1)=xi;
y(n,1)=yi;
end
t=1:n;
ts=1:0.1:n;
xs=spline(t,x,ts);
ys=spline(t,y,ts);
plot(xs,ys,’r-’);
hold off
实例15:变换的傅立叶函数曲线
function shili15
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例15’);
axis equal
m=moviein(20,gcf);
set(gca,’nextplot’,’replacechildren’)
h=uicontrol(’style’,’slider’,’position’,...
,’min’,1,’max’,20)
for j=1:20
plot(fft(eye(j+16)))
set(h,’value’,j)
m(:,j)=getframe(gcf);
end
clf;
axes(’position’,);
movie(m,30)
实例16:劳伦兹非线形方程的无序活动
function shili15
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例15’);
axis equal
m=moviein(20,gcf);
set(gca,’nextplot’,’replacechildren’)
h=uicontrol(’style’,’slider’,’position’,...
,’min’,1,’max’,20)
for j=1:20
plot(fft(eye(j+16)))
set(h,’value’,j)
m(:,j)=getframe(gcf);
end
clf;
axes(’position’,);
movie(m,30)
实例17:填充图
function shili17
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例17’);
t=(1:2:15)*pi/8;
x=sin(t);
y=cos(t);
fill(x,y,’r’)
axis square off
text(0,0,’STOP’,...
’color’,,...
’fontsize’,50,...
’horizontalalignment’,’center’)
例18:条形图和阶梯形图
function shili18
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例18’);
subplot(2,2,1)
x=-3:0.2:3;
y=exp(-x.*x);
bar(x,y)
title(’2-D Bar Chart’)
subplot(2,2,2)
x=-3:0.2:3;
y=exp(-x.*x);
bar3(x,y,’r’)
title(’3-D Bar Chart’)
subplot(2,2,3)
x=-3:0.2:3;
y=exp(-x.*x);
stairs(x,y)
title(’Stair Chart’)
subplot(2,2,4)
x=-3:0.2:3;
y=exp(-x.*x);
barh(x,y)
title(’Horizontal Bar Chart’)
实例19:三维曲线图
function shili19
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例19’);
subplot(2,1,1)
x=linspace(0,2*pi);
y1=sin(x);
y2=cos(x);
y3=sin(x)+cos(x);
z1=zeros(size(x));
z2=0.5*z1;
z3=z1;
plot3(x,y1,z1,x,y2,z2,x,y3,z3)
grid on
xlabel(’X轴’);
ylabel(’Y轴’);
zlabel(’Z轴’);
title(’Figure1:3-D Plot’)
subplot(2,1,2)
x=linspace(0,2*pi);
y1=sin(x);
y2=cos(x);
y3=sin(x)+cos(x);
z1=zeros(size(x));
z2=0.5*z1;
z3=z1;
plot3(x,z1,y1,x,z2,y2,x,z3,y3)
grid on
xlabel(’X轴’);
ylabel(’Y轴’);
zlabel(’Z轴’);
title(’Figure2:3-D Plot’)
实例20:图形的隐藏属性
function shili20
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例20’);
subplot(1,2,1)
=sphere(10);
mesh(x,y,z)
axis off
title(’Figure1:Opaque’)
hidden on
subplot(1,2,2)
=sphere(10);
mesh(x,y,z)
axis off
title(’Figure2:Transparent’)
hidden off
实例21PEAKS函数曲线
function shili21
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例21’);
=peaks(30);
subplot(2,1,1)
x=x(1,:);
y=y(:,1);
i=find(y》0.8&y《1.2);
j=find(x》-0.6&x《0.5);
z(i,j)=nan*z(i,j);
surfc(x,y,z)
xlabel(’X轴’);
ylabel(’Y轴’);
zlabel(’Z轴’);
title(’Figure1:surfc函数形成的曲面’)
subplot(2,1,2)
x=x(1,:);
y=y(:,1);
i=find(y》0.8&y《1.2);
j=find(x》-0.6&x《0.5);
z(i,j)=nan*z(i,j);
surfl(x,y,z)
xlabel(’X轴’);
ylabel(’Y轴’);
zlabel(’Z轴’);
title(’Figure2:surfl函数形成的曲面’)
实例22:片状图
function shili22
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例22’);
subplot(1,2,1)
x=rand(1,20);
y=rand(1,20);
z=peaks(x,y*pi);
t=delaunay(x,y);
trimesh(t,x,y,z)
hidden off
title(’Figure1:Triangular Surface Plot’);
subplot(1,2,2)
x=rand(1,20);
y=rand(1,20);
z=peaks(x,y*pi);
t=delaunay(x,y);
trisurf(t,x,y,z)
title(’Figure1:Triangular Surface Plot’);
实例23:视角的调整
function shili23
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例23’);
x=-5:0.5:5;
=meshgrid(x);
r=sqrt(x.^2+y.^2)+eps;
z=sin(r)./r;
subplot(2,2,1)
surf(x,y,z)
xlabel(’X-axis’)
ylabel(’Y-axis’)
zlabel(’Z-axis’)
title(’Figure1’)
view(-37.5,30)
subplot(2,2,2)
surf(x,y,z)
xlabel(’X-axis’)
ylabel(’Y-axis’)
zlabel(’Z-axis’)
title(’Figure2’)
view(-37.5+90,30)
subplot(2,2,3)
surf(x,y,z)
xlabel(’X-axis’)
ylabel(’Y-axis’)
zlabel(’Z-axis’)
title(’Figure3’)
view(-37.5,60)
subplot(2,2,4)
surf(x,y,z)
xlabel(’X-axis’)
ylabel(’Y-axis’)
zlabel(’Z-axis’)
title(’Figure4’)
view(180,0)
实例24:向量场的绘制
function shili24
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例24’);
subplot(2,2,1)
z=peaks;
ribbon(z)
title(’Figure1’)
subplot(2,2,2)
=peaks(15);
=gradient(z,0.5,0.5);
contour(x,y,z,10)
hold on
quiver(x,y,dx,dy)
hold off
title(’Figure2’)
subplot(2,2,3)
=peaks(15);
=surfnorm(x,y,z);
surf(x,y,z)
hold on
quiver3(x,y,z,nx,ny,nz)
hold off
title(’Figure3’)
subplot(2,2,4)
x=rand(3,5);
y=rand(3,5);
z=rand(3,5);
c=rand(3,5);
fill3(x,y,z,c)
grid on
title(’Figure4’)
实例25:灯光定位
function shili25
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例25’);
vert=[1 1 1;1 2 1;
2 2 1;2 1 1;
1 1 2;1 2 2;
2 2 2;2 1 2];
fac=[1 2 3 4;2 6 7 3;
4 3 7 8;1 5 8 4;
1 2 6 5;5 6 7 8];
grid off
sphere(36)
h=findobj(’type’,’surface’);
set(h,’facelighting’,’phong’,...
’facecolor’,...
’interp’,...
’edgecolor’,,...
’backfacelighting’,...
’lit’)
hold on
patch(’faces’,fac,’vertices’,vert,...
’facecolor’,’y’);
light(’position’,);
light(’position’,);
material shiny
axis vis3d off
hold off
实例26:柱状图
function shili26
h0=figure(’toolbar’,’none’,...
’position’,,...
’name’,’实例26’);
subplot(2,1,1)
x=[5 2 1
8 7 3
9 8 6
5 5 5
4 3 2];
bar(x)
xlabel(’X轴’);
ylabel(’Y轴’);
title(’第一子图’);
subplot(2,1,2)
y=[5 2 1
8 7 3
9 8 6
5 5 5
4 3 2];
barh(y)
xlabel(’X轴’);
ylabel(’Y轴’);
title(’第二子图’);
急需一道matlab程序设计题,至少30行,能运行出来的,另外能解释一下这个程序是干什么的,谢谢了
1、对函数 在n个节点上用拉格朗日、分段线性、三次样条三种插值方法,计算m个插值点的函数值,通过对函数和图形的输出,将三种插值结果与精确值进行比较,适当增加n,再作比较,由此做出初步分析。
解:
Step1、
Matlab 计算源程序:
%拉格朗日插值
function y=lagr(x0,y0,x)
n=length(x0);m=length(x);
for i=1:m
z=x(i);
s=0;
for k=1:n
p=1;
for j=1:n
if j~=k
p=p*(z-x0(j))/(x0(k)-x0(j));
end
end
s=p*y0(k)+s;
end
y(i)=s;
end
clear all;
x0=-1:0.5:1; %n=5,产生节点列x0,y0
y0=(1-x0.^2).^0.5;
x=-1:0.02:1; %产生插值节点列x,间隔0.02
y=(1-x.^2).^0.5; %计算精确值
y1=lagr(x0,y0,x); %计算拉格朗日插值
y2=interp1(x0,y0,x);%计算分段线性插值
y3=spline(x0,y0,x); %计算三次样条插值
plot(x,y,’k-’,x,y1,’b+’,x,y2,’g*’,x,y3,’r’);%将4条曲线绘在同一图中比较
gtext(’黑色:y=sqrt(1-x0.^2)’,’FontSize’,12);
gtext(’蓝色:Lagr.’,’FontSize’,12);
gtext(’绿色:Pies.Lin.’,’FontSize’,12);
gtext(’红色:Spline’,’FontSize’,12);
a1=0; %提取20个数据进行比较,并计算平均平方误差
a2=0;
a3=0;
for k=1:20
xx(k)=x(5*k-1);
yy(k)=y(5*k-1);
yy1(k)=y1(5*k-1);
yy2(k)=y2(5*k-1);
yy3(k)=y3(5*k-1);
a1=a1+(yy1(k)-yy(k))^2;
a2=a2+(yy2(k)-yy(k))^2;
a3=a3+(yy3(k)-yy(k))^2;
end
A=./20;
A
【结果计算】
数值比较如下:
x y y1 y2 y3
-0.9400 0.3412 0.1800 0.1039 0.1656
-0.8400 0.5426 0.4229 0.2771 0.3992
-0.7400 0.6726 0.6057 0.4503 0.5846
-0.6400 0.7684 0.7400 0.6235 0.7273
-0.5400 0.8417 0.8362 0.7967 0.8329
-0.4400 0.8980 0.9030 0.8821 0.9070
-0.3400 0.9404 0.9477 0.9089 0.9552
-0.2400 0.9708 0.9760 0.9357 0.9830
-0.1400 0.9902 0.9923 0.9625 0.9960
-0.0400 0.9992 0.9994 0.9893 0.9998
0.0600 0.9982 0.9986 0.9839 0.9995
0.1600 0.9871 0.9898 0.9571 0.9944
0.2600 0.9656 0.9714 0.9303 0.9788
0.3600 0.9330 0.9402 0.9035 0.9474
0.4600 0.8879 0.8916 0.8767 0.8945
0.5600 0.8285 0.8196 0.7621 0.8145
0.6600 0.7513 0.7165 0.5889 0.7019
0.7600 0.6499 0.5734 0.4157 0.5511
0.8600 0.5103 0.3796 0.2425 0.3565
0.9600 0.2800 0.1231 0.0693 0.1126
平均平方误差:
A =
0.0047 0.0204 0.0063
根据以上数据可计算出各种插值方法算出值与精确值之间的平均平方误差: (n=20)
拉格朗日插值法:
线性插值法:
三次样条插值法:
Step2、
改变n值。
Matlab源程序:
x0=-1:0.2:1; %n=11,产生节点列x0,y0
y0=(1-x0.^2).^0.5;
x=-1:0.02:1; %产生插值节点列x,间隔0.02
y=(1-x.^2).^0.5; %计算精确值
y1=lagr(x0,y0,x); %计算拉格朗日插值
y2=interp1(x0,y0,x);%计算分段线性插值
y3=spline(x0,y0,x); %计算三次样条插值
plot(x,y,’k-’,x,y1,’b+’,x,y2,’g*’,x,y3,’r’);%将4条曲线绘在同一图中比较
gtext(’黑色:y=sqrt(1-x0.^2)’,’FontSize’,12);
gtext(’蓝色:Lagr.’,’FontSize’,12);
gtext(’绿色:Pies.Lin.’,’FontSize’,12);
gtext(’红色:Spline’,’FontSize’,12);
a1=0; %提取20个数据进行比较,并计算平均平方误差
a2=0;
a3=0;
for k=1:20
xx(k)=x(5*k-1);
yy(k)=y(5*k-1);
yy1(k)=y1(5*k-1);
yy2(k)=y2(5*k-1);
yy3(k)=y3(5*k-1);
a1=a1+(yy1(k)-yy(k))^2;
a2=a2+(yy2(k)-yy(k))^2;
a3=a3+(yy3(k)-yy(k))^2;
end
A=./20;
A
【结果计算】
数值结果为:
x y y1 y2 y3
-0.9400 0.3412 0.2829 0.1800 0.2453
-0.8400 0.5426 0.5368 0.4800 0.5245
-0.7400 0.6726 0.6746 0.6600 0.6842
-0.6400 0.7684 0.7688 0.7600 0.7732
-0.5400 0.8417 0.8414 0.8350 0.8386
-0.4400 0.8980 0.8979 0.8932 0.8968
-0.3400 0.9404 0.9405 0.9355 0.9413
-0.2400 0.9708 0.9708 0.9671 0.9711
-0.1400 0.9902 0.9901 0.9859 0.9900
-0.0400 0.9992 0.9992 0.9960 0.9992
0.0600 0.9982 0.9982 0.9939 0.9981
0.1600 0.9871 0.9871 0.9838 0.9870
0.2600 0.9656 0.9656 0.9608 0.9661
0.3600 0.9330 0.9330 0.9292 0.9336
0.4600 0.8879 0.8878 0.8816 0.8860
0.5600 0.8285 0.8283 0.8233 0.8259
0.6600 0.7513 0.7521 0.7400 0.7587
0.7600 0.6499 0.6518 0.6400 0.6595
0.8600 0.5103 0.4992 0.4200 0.4798
0.9600 0.2800 0.2040 0.1200 0.1708
A =
0.0005 0.0032 0.0011
根据以上数据可计算出各种插值方法算出值与精确值之间的平均平方误差:
拉格朗日插值法:
线性插值法:
三次样条插值法:
【结果分析与讨论】
1、由图形和数值分析都能看出插值与精确函数值的接近度由好到坏依次为:拉格朗日插值、三次样条插值、分段线性插值,和书中三次样条插值、分段线性插值、拉格朗日插值的结论不同。可见,拉格朗日插值法在收敛的情况下,与真实值还是很接近的,但是在一些情况下,插值不收敛于函数值,出现龙格振荡。
2、由图线和数值(平均平方误差)可见节点数越多(n越大),插值与真实值越接近。

更多文章:
在from子句中可以出现(如何在from 子句中嵌套查询下面的语句在access中出错!)
2026年10月11日 05:20
countif函数统计个数怎么用(countif函数怎么用 详解Excel中countif函数的使用方法)
2026年10月11日 03:30
正则匹配数字之前的字符(正则表达式如何匹配前面是数字、中间是“/”、后面也是数字,就像2/3专业的模式)
2026年10月11日 03:00
orlnsertbootmediinselected(我电脑开机显示这个是什么意思or insert boot media in select)
2026年10月10日 23:00
display的用法(display是什么意思 详解display的含义和用法)
2026年10月10日 22:00
html全部居中代码(怎么让网页居中显示,html如何让网页居中)
2026年10月10日 21:10





