matlab用于计算方法的源程序
matlab实现数值计算功能源程序(个人整理)
matlab数值计算功能1,基础运算(1)多项式的创建与表达将多项式(x-6)(x-3)(x-8)表示为系数形式a=[6 3 8] % 写成根矢量pa=poly(a)% 求出系数矢量ppa=poly2sym(pa,'x') % 表示成符号形式ezplot(ppa,[-50,50])求3介方阵A的特征多项式a=[6 2 4;7 5 6;1 3 6 ];pa=poly(a)% 写出系数矢量ppa=poly2sym(pa) %表示成符号形式ezplot(ppa,[-50,50]) % 绘图求x^3-6x^2-72x-27的根。
a=[1,-6,-72,-85]; % 写出多项式系数矢量r=roots(a) % 求多项式的根(2)多项式的乘除运算c=conv(a,b) %乘法[q,r]=deconv(c,a)% 除法求a(s)=s^2+2s+3乘以b(s)=4s^2+5s+6的乘积a=[1 2 3]b=[4 5 6] % 写出系数矢量c=conv(a,b)c=poly2sym(c,'s') % 写成符号形式的多项式展开(s^2+2s+2)(s+4)(s+1)并验证结果被(s+4),(s+3)除后的结果。
c=conv([1,2,2],conv([1,4],[1,1]))cs=poly2sym(c,'s')c=[1 7 16 18 8][q1,r1]=deconv(c,[1,4])[q2,r2]=deconv(c,[1,3])cc=conv(q2,[1,3])test=((c-r2)==cc)其他常用的多项式运算命令pa=polyval(p,s) % 按数组规则计算给定s时多项式的值pm=polyvalm(p,s)% 按矩阵规则计算给定s时多项式的值[r,p,k]=residue(b,a) % 部分分式展开,b,a分别是分子,分母多项式系数矢量。
r,p,k分别是留数,极点和值项矢量。
计算方法matlab上机报告(流程图、源代码等)
上机报告1. 共轭梯度法(1)计算过程如下:第一步:取初始向量)0(X , 计算⎪⎪⎩⎪⎪⎨⎧+=><><-=-=-∇==(0)0(0)(1))0()0()0()0(0(0)(0)(0)(0)d X X ,,X )X (r d λλAd d Ad r A b f 第1+k 步:计算⎪⎪⎪⎪⎪⎩⎪⎪⎪⎪⎪⎨⎧+=><><-=+=><><-=-=-∇=+------(k)0(k)1)(k )()()()()1(1(k))()1()1()1()(1(k)(k)(k)d X X,,r ,,X )X (r λλββk k k k k k k k k k k k k Ad d Ad r d d Ad d Ad r A b f(2)syms n; %定义一个变量nA=input('请输入矩阵A')b=input('请输入矩阵b')n=size(A)X=zeros(n);D=zeros(n);R=zeros(n);x=X(:,1); %将矩阵X 的第一列赋值给x 作为初始向量r=b-A*x; %将x 代入求得初始非零残向量R(:,1)=r;d=r; %求得初始方向向量D(:,1)=d;for i=1:n; %利用循环进行迭代求得各向量AF(i)=R(:,i)'*R(:,i)/(D(:,i)'*A*D(:,i));X(:,i+1)= X(:,i)+AF(i)*D(:,i);R(:,i+1)=b-A*X(:,i+1);BT(i)=norm(R(:,i+1))^2/norm(R(:,i))^2;D(:,i+1)=R(:,i+1)+BT(i)*D(:,i) ;endx=X(:,i+1) %输出计算结果2.龙贝格积分s=input('请输入被积函数表达式:f(x)=','s') %键盘输入被积函数表达式f=inline(s)a=input('请输入积分下限a=')%输入积分下限ab=input('请输入积分上限b=')%输入积分上限bf0=f(a);%求下限值f(a)f1=f(b);%求上限值f(b)j=zeros(8,4);%定义一个矩阵用来存放T,S,C,R值j(1,1)=(b-a)*0.5*(f0+f1);%计算出T1for i=2:8;%对i循环赋值t=2^(i-2);fj=zeros(t,1);%构造一个矩阵为接下来存放f(a+(2i-1)*(b-a)/2^(k+1))的值做准备for m=1:t;fj(m,1)=f(a+(2*m-1)*(b-a)/2^(i-1));%将计算得到的f(a+(2i-1)*(b-a)/2^(k+1))的值赋值给对应的矩阵元素ff=sum(fj);%对矩阵所有元素求和endj(i,1)=0.5*j(i-1,1)+(1/2^(i-1))*ff;%得到所有的T值j(i,2)=j(i,1)+(1/3)*(j(i,1)-j(i-1,1));%得到所有的S值j(i,3)=j(i,2)+(1/3)*(j(i,2)-j(i-1,2));%得到所有的C值j(i,4)=j(i,3)+(1/3)*(j(i,3)-j(i-1,3));%得到所有的R值s=j(i,4)-j(i-1,4);endj %输出计算结果表If=vpa(j(i,4),7) %得到精确的积分结果3.三样条插值X=zeros(1,6) ; %定义一些下面将用到的矩阵y=zeros(1,6);f=zeros(6);l=zeros(1,6);for i=1:1:6X(i)=((i-1)*2)/5-1; %将区间等分取点y(i)=1/(1+25*X(i)*X(i)); %得到对应点的函数值endfor j=2:6f(1,1)= y(1);f(j, 1)=y(j);for k=2:jf(j,k)=(f(j,k-1)-f(j-1,k-1))/(X(j)-X(j-k+1)); %利用循环求差商endendf %得到差商表,其中对角线上的为需要的差商值syms x;l=[x,x,x,x,x,x];g=l-X;n=g;for t=2:6n(1,t)= n(1,t-1)* n(1,t);endN=zeros(1,1);N=f(1,1);for r=1:5N= N +n(1,r)*f(r+1,r+1); %利用循环求5次牛顿插值多项式endN %得到5次牛顿插值多项式P=zeros(1,101);X1=[-1:0.02:1]; %取间距为0.02的等分点作为作图的横坐标for u=1:101x=X1(u);subs(N) ;p(1,u)= subs(N(1,1)) ; %用上面所求出的5次牛顿插值多项式算得所取等分点的函数值作为纵坐标endplot(X1,p, '-c') %绘制5次牛顿插值曲线hold onXX=zeros(1,11) ; %定义一些下面将用到的矩阵yy=zeros(1,11);F=zeros(11);L=zeros(1,11);for I=1:1:11XX(I)=((I-1)*2)/10-1; %将区间等分取点yy(I)=1/(1+25*XX(I)*XX(I)); %得到对应点的函数值endfor J=2:11F(1,1)= yy(1);F(J, 1)=yy(J);for K=2:JF(J,K)=(F(J,K-1)-F(J-1,K-1))/(XX(J)-XX(J-K+1)); %利用循环求差商endendF %得到差商表,其中对角线上的为需要用的差商值syms x1;L=[x1,x1,x1,x1,x1,x1,x1,x1,x1,x1,x1];G=L-XX;M=G;for T=2:11M(1,T)= M(1,T-1)* M(1,T);endH=F(1,1);for R=1:10H= H +M(1,R)*F(R+1,R+1); %利用循环求10次牛顿插值多项式endH %得到10次牛顿插值多项式P=zeros(1,101);XX1=[-1:0.02:1]; %取间距为0.02的等分点作为作图的横坐标for u1=1:101x1=XX1(u1);subs(H) ;P(1,u1)= subs(H) ; %用上面所求出的10次牛顿插值多项式算得所取等分点的函数值作为纵坐标endplot(XX1,P, '-g')hold on %绘制10次牛顿插值曲线x3=[-1:0.02:1]; %取间距为0.02的等分点作为作图的横坐标y3=1./(1+25*x3.^2); %用原函数计算得到所取等分点的纵坐标plot(x3,y3, '-k')hold on %绘制原函数f(x)的曲线h=0.2;u=0.5;z=0.5;d=zeros(11,1);d(1)=6*((yy(2)-yy(1))/0.2-50/(26*26))/0.2;d(11)=6*(-50/(26*26)-(yy(11)-yy(10))/0.2)/0.2;a=0.5*ones(1,10);b=2*ones(1,11);c=diag(a,1)+diag(a,-1)+diag(b);c(1,2)=1;c(11,10)=1;e=zeros(11,1); %得到三次样条插值第二种边界条件对应的严格三对角占优矩阵e(1,1)=d(1);e(11,1)=d(11);for ii=2:10e(ii,1)=F(ii+1,3);ende; %得到d0,d1,….dn用矩阵e表示ff=horzcat(c,e) %利用d0,d1,…..dn与上面所得的三对角占优矩阵构造增广矩阵ff y0=zeros(11,1); %定义一些下面将要用的矩阵uu=zeros(11,12);uu(1,1)=ff(1,1);ll=zeros(11,11);ll(1,1)=1;y0(1,1)=ff(1,12);for tt=2:11ll(tt,tt)=1;ll(tt,1)=ff(tt,1)/ uu(1,1);l(tt,tt-1)=ff(tt,tt-1) /uu(tt-1,tt-1);for jj=tt:12uu(1,jj)= ff(1,jj);uu(tt,jj)=ff(tt,jj)- ll(tt,tt-1)*uu(tt-1,jj); %对增广矩阵进行LU分解,同时进行追法求y0 y0(tt,1)=uu(tt,12);endendxx=zeros(11,1);for kk=10:-1:1xx(11,1)=y0(11,1)/uu(11,12);xx(kk,1)=(y0(kk,1)-uu(kk,kk+1)*xx(kk+1,1))/uu(kk,kk); %用赶法求得xxendM0=xx;Sx=zeros(10,1);for i0=1:10syms x2;Sx=sym(Sx);Sx(i0)= ((XX(i0+1)-x2)*(XX(i0+1)-x2)*(XX(i0+1)-x2)/(6*h))*M0(i0)+...((x2-XX(i0))*(x2-XX(i0))*(x2-XX(i0))/(6*h))*M0(i0+1)+ ...(yy(i0)-(h*h/6)*M0(i0))*(XX(i0+1)-x2)/h+...(yy(i0+1)-(h*h/6)*M0(i0+1))*(x2-XX(i0))/h; %利用循环求不同区间段的三样条插值多项式endSx %得到不同区间段的三样条插值多项式,用矩阵表示w1=[XX(1):0.01:XX(2)]; %将第一段等分曲线段等分成20段,得各等分点横坐标P1=zeros(1,21);for t1=1:21x2=w1(t1);subs(Sx(1));P1(1,t1)= subs(Sx(1)); %求得第一段等分曲线段等分成20段的各等分点横坐标对应的纵坐标endplot(w1,P1) %绘制利用三样条插值多项式所得的第一等分段曲线hold onw2=[XX(2):0.01:XX(3)]; %将第2段等分曲线段等分成20段,得各等分点横坐标P2=zeros(1,21);for t2=1:21x2=w2(t2);subs(Sx(2));P2(1,t2)= subs(Sx(2)); %求得第2段等分曲线段等分成20段的各等分点横坐标对应的纵坐标endplot(w2,P2) %绘制利用三样条插值多项式所得的第2等分段曲线hold onw3=[XX(3):0.01:XX(4)]; %将第3段等分曲线段等分成20段,得各等分点横坐标P3=zeros(1,21);for t3=1:21x2=w3(t3);subs(Sx(3));P3(1,t3)= subs(Sx(3)); %求得第3段等分曲线段等分成20段的各等分点横坐标对应的纵坐标endplot(w3,P3) %绘制利用三样条插值多项式所得的第3等分段曲线hold onw4=[XX(4):0.01:XX(5)]; %将第4段等分曲线段等分成20段,得各等分点横坐标P4=zeros(1,21);for t4=1:21x2=w4(t4);subs(Sx(4));P4(1,t4)= subs(Sx(4)); %求得第4段等分曲线段等分成20段的各等分点横坐标对应的纵坐标endplot(w4,P4) %绘制利用三样条插值多项式所得的第4等分段曲线hold onw5=[XX(5):0.005:XX(6)]; %将第5段等分曲线段等分成40段,得各等分点横坐标P5=zeros(1,41);for t5=1:41x2=w5(t5);subs(Sx(5));P5(1,t5)= subs(Sx(5)); %求得第5段等分曲线段等分成40段的各等分点横坐标对应的纵坐标endplot(w5,P5) %绘制利用三样条插值多项式所得的第5等分段曲线hold on%以下依次绘制其余各等分段的曲线w6=[XX(6):0.005:XX(7)];P6=zeros(1,41);for t6=1:41x2=w6(t6);subs(Sx(6));P6(1,t6)= subs(Sx(6));endplot(w6,P6) %利用三样条插值多项式绘制第6等分段曲线hold onw7=[XX(7):0.01:XX(8)];P7=zeros(1, 21);for t7=1:21x2=w7(t7);subs(Sx(7));P7(1,t7)= subs(Sx(7));endplot(w7,P7) %利用三样条插值多项式绘制第7等分段曲线hold onw8=[XX(8):0.01:XX(9)];P8=zeros(1,21);for t8=1:21x2=w8(t8);subs(Sx(8));P8(1,t8)= subs(Sx(8));endplot(w8,P8) %利用三样条插值多项式绘制第8等分段曲线hold onw9=[XX(9):0.01:XX(10)];P9=zeros(1,21);for t9=1:21x2=w9(t9);subs(Sx(9));P9(1,t9)= subs(Sx(9));endplot(w9,P9) %利用三样条插值多项式绘制第9等分段曲线hold onw10=[XX(10):0.01:XX(11)];P10=zeros(1,21);for t10=1:21x2=w10(t10);subs(Sx(10));P10(1,t10)= subs(Sx(10));endplot(w10,P10) %利用三样条插值多项式绘制第10等分段曲线4.龙格库塔法%龙格库塔法求3阶常微分方程的初值问题的通用程序h=input('请输入步长值h=')%通过键盘输入步长ha=input('请输入给定的x的初始点值a=')%通过键盘输入起始点的x值b=input('请输入要求近似值的点的x值b=')%通过键盘输入所求近似值点的x值n=(b-a)/h%求得步长个数s=input('请输入m阶微分方程转化为一阶微分方程的后的表达式f(x,y1,y2,y3)=','s')%输入3阶常微分方程转化为一阶常微分方程后的表达式f=inline(s)y=zeros(3,n+1);%定义一个矩阵来存放循环求得的y1,y2,y3值k1=zeros(3,n);%定义一个矩阵来存放循环求得的k1值k2=zeros(3,n);%定义一个矩阵来存放循环求得的k2值k3=zeros(3,n);%定义一个矩阵来存放循环求得的k3值k4=zeros(3,n);%定义一个矩阵来存放循环求得的k4值A=input('请将微分方程在初始点a的不同阶的的值按阶数由低到高输入到矩阵中A中构成一个3x1的列矩阵A=')%将给定的初值输入到列矩阵A中y(:,1)=A;%将y1,y2,y3的初值赋值给矩阵y的第一列for i=2:n+1%引入循环k1(1,i-1)=h*y(2,i-1);%写出k1矩阵第一行的数学表达式k1(2,i-1)=h*y(3,i-1);%写出k1矩阵第二行的数学表达式k1(3,i-1)=h*f(a,y(1,i-1),y(2,i-1),y(3,i-1));%写出k1矩阵第三行的数学表达式k2(1,i-1)=h*(y(2,i-1)+k1(2,i-1)/2);%写出k2矩阵第一行的数学表达式k2(2,i-1)=h*(y(3,i-1)+k1(3,i-1)/2);%写出k2矩阵第二行的数学表达式k2(3,i-1)=h*f(a+((i-2)*h+h/2),y(1,i-1)+k1(1,i-1)/2,y(2,i-1)+k1(2,i-1)/2,y(3,i-1)+k1(3,i-1)/2);%写出k2矩阵第三行的数学表达式k3(1,i-1)=h*(y(2,i-1)+k2(2,i-1)/2);%写出k3矩阵第一行的数学表达式k3(2,i-1)=h*(y(3,i-1)+k2(3,i-1)/2);%写出k3矩阵第二行的数学表达式k3(3,i-1)=h*f(a+((i-2)*h+h/2),y(1,i-1)+k2(1,i-1)/2,y(2,i-1)+k2(2,i-1)/2,y(3,i-1)+k2(3,i-1)/2);%写出k3矩阵第三行的数学表达式k4(1,i-1)=h*(y(2,i-1)+k3(2,i-1));%写出k4矩阵第一行的数学表达式k4(2,i-1)=h*(y(3,i-1)+k3(3,i-1));%写出k4矩阵第二行的数学表达式k4(3,i-1)=h*f(a+((i-2)*h+h),y(1,i-1)+k3(1,i-1),y(2,i-1)+k3(2,i-1),y(3,i-1)+k3(3,i-1));%写出k4矩阵第三行的数学表达式y(1,i)=y(1,i-1)+(1/6)*(k1(1,i-1)+2*k2(1,i-1)+2*k3(1,i-1)+k4(1,i-1));%利用循环求得矩阵y的第一行y(2,i)=y(2,i-1)+(1/6)*(k1(2,i-1)+2*k2(2,i-1)+2*k3(2,i-1)+k4(2,i-1));%利用循环求得矩阵y的第二行y(3,i)=y(3,i-1)+(1/6)*(k1(3,i-1)+2*k2(3,i-1)+2*k3(3,i-1)+k4(3,i-1));%利用循环求得矩阵y的第三行endy %得到矩阵yjg=y(1,n+1) % y(b)的近似值=y(1,n+1),所求的近似解与解析解值很接近。
ICA快速算法原理和matlab算法程序
顿迭代法解方程(3.8) 。用 F 表示式(3.8)左边的函数,可得 F 的雅可比矩阵 JF W 如 下:
JF W E XX T g ' W T X I
(2.9)
为了简化矩阵的求逆,可以近似为(3.9)式的第一项。由于数据被球化, E XX T I , 所 以,E XX T g ' W T X
Y=WP'*Z; G=Y.^3;%G为非线性函数,可取y^3等 GG=3*Y.^2; %G的导数 count=0; LastWP=zeros(m,1); W(:,n)=W(:,n)/norm(W(:,n)); while abs(WP-LastWP)&abs(WP+LastWP)>Critical count=count+1; %迭代次数 LastWP=WP; %上次迭代的值 % WP=1/T*Z*((LastWP'*Z).^3)'-3*LastWP; for i=1:m WP(i)=mean(Z(i,:).*(tanh((LastWP)'*Z)))-(mean(1-(tanh((
(2.10)
这里, W 是 W 的新值, E W T Xg W T X ,规格化能提高解的稳定性。简化后就可 以得到 FastICA 算法的迭代公式:
W E Xg W T X E g ' W T X W W W / W
T
T
I ,其中: I 为单位矩阵,我
T
们称这个向量为白化向量。白化的本质在于去相关,这同主分量分析的目标是一样的。在 ICA 中 , 对 于 为 零 均 值 的 独 立 源 信 号 S t S1 t ,..., S N t , 有 :
一维非定常热传导方程的求解及matlab源程序
一维非定常热传导方程的求解及matlab 源程序1、计算模型本题计算的模型示意图如图1所示,在已知两边界点温度数值的情况下,根据一维非定常热传导方程,求解整个计算域长度上的温度分布。
一维非定常热传导方程为0x Tt T 22=∂∂⋅-∂∂α,式中α=1。
总长为10m ,两端的边界数值分别为T0=100℃和Tn=300℃。
计算域内的热传导满足方程:∂T ∂t −∂∙∂2T∂X2=0图1 一维非定常热传导方程计算模型示意图2、数值分析方法在本题的计算过程中,用到的数值分析方法有:差分近似导数,追赶法解三对角方程组。
对于一维非定常热传导而言,热传导参数的分布是连续的,具有无穷多个数值,它们的数值由给定的非定常热传导方程决定。
但是微分方程无法直接求解,因此通过差分近似导数的方法,将微分方程转化成代数方程,然后通过迭代即可计算出平衡时刻各个参考点的温度。
在计算时,先由一维非定常热传导的微分方程,推导出与其对应的线性方程,将第i 个时间层上某个离散点处的温度用第i-1个时间层上某些点的温度数值来表示。
这样在求解过程中,先假定第0时间层的时刻各参考点的温度初值,然后运用线性方程组推导出第1时间层时刻,各个参考点的温度数值,再求第2时间层时刻各个参考点处的温度数值,依次类推,直到相邻时间层上的速度残差达到预先设定的收敛要求为止。
此计算模型中给定的左边界温度值为T0=100℃,右边界温度值为Tn=300℃,均恒定不变。
总长度10m 进行N 等分。
3、数值计算过程一维非定常热传导方程为:0x T t T 22=∂∂⋅-∂∂α,移项处理得:22xT t T ∂∂⋅=∂∂α一阶向前差分:t T ∂∂=t 1n ∆-+nii T T ;二阶中心差分:()211222x T x T T T ni n i n i ∆+⋅-⋅∂=∂∂⋅∂-+; 因此可化简为代数方程为:()2111n 2t x T T T T T ni n i n i n i i ∆+⋅-⋅∂=∆--++ ; 即 ()()nin i n i n i iT T T T x tT ++⋅-⋅∆∆⋅∂=-++1121n 2;用()n i n i T T 11121++++代替n i T 1+,用()n i n i T T ⋅-⋅-+22211代替n i T ⋅-2,用()ni n i T T 11121-+-+代替n i T 1-,即: ()()()()()⎥⎦⎤⎢⎣⎡++⋅-⋅-++⋅∆∂=∆+⋅-⋅∂=∆--+-++++-++n i n i n i n i n i n i n i n i n i n i i T T T T T T x x T T T T T 111111122111n 212221212t ()()()()()n i n i n i n i n i n i n i T T T x t T T x t T x t Tx t 1121121211222212-+++++-+⋅-⋅∆⋅∆⋅∂--=⋅∆⋅∆⋅∂+⋅⎪⎪⎭⎫ ⎝⎛∆∆⋅∂+-⋅∆⋅∆⋅∂因此,上式的含义是:第n+1个时间层上第i-1个,第i 个和第i+1个参考点处的温度值,与第n 个时间层上第i-1个,第i 个和第i+1个参考点处的温度值的关系式;代数关系式示意图如图2所示:图2代数方程式示意图令A=()22x t ∆⋅∆⋅∂,B=()21x t ∆∆⋅∂+ ,Ki=()()n i n i n i n i T T T x t T 11222-++⋅-⋅∆⋅∆⋅∂-- 则上式可化简为:Ki T A T B T A n i n i n i =⋅+⋅-⋅++++-11111即2321K T A T B T A =⋅+⋅-⋅'21232K T A K T A T B =⋅-=⋅+⋅-3432K T A T B T A =⋅+⋅-⋅112---=⋅+⋅-⋅n n n n K T A T B T A'1112----=⋅-=+⋅-⋅n n n n n K T A K T B T A⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎣⎡=⎥⎥⎥⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎢⎢⎢⎣⎡⋅⎥⎥⎥⎥⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎢⎢⎢⎢⎣⎡--'16543'2165432B -A 0 0 0 0 0 00 0A B -A 0 0 00 0 0A B -A 0 00 0 0 0A B -A 00 0 0 0 0A B -A 0 0 0 0 0 0A B -n n K K K K K K T T T T T T当第0时间层时刻各个参考点温度已知时,方程式右侧的矩阵[K2’ K3 K4 K5 ……Kn-2 Kn-1’] ‘为已知量,系数矩阵U 也为已知量,则可以计算出第1时间层各个参考点的温度值[T2 T3 T4 T5 T6……Tn-1] ‘,并且T1=100,Tn=300。
FDTD(时域有限差分法)算法的Matlab源程序
rect=get(gcf,'Position'); rect(1:2)=[0 0];
M=moviein(nmax/2,gcf,rect);
%*********************************************************************** % BEGIN TIME-STEPPING LOOP %***********************************************************************
%
%***********************************************************************
clear
%*********************************************************************** % Fundamental constants
% Gaussian pulse given by
matlab ode78源程序
MATLAB是一种广泛用于科学计算和工程设计的高级编程语言和交互式环境。
其ODE78函数是MATLAB中用于求解常微分方程组的函数,能够高效地求解一般形式的刚性和非刚性常微分方程组。
本文将介绍ODE78函数的源程序以及其相关知识。
一、ODE78函数介绍1. ODE78函数是MATLAB中的一个常微分方程(ODE)求解器,可用于解决形如dy/dx=f(x,y)的一阶和高阶ODE。
2. ODE78函数利用Adams-Bashforth-Moulton方法和变步长算法实现了高效的ODE求解。
3. ODE78函数支持对ODE方程的自定义,并可通过设置选项来调整算法参数以获得更精确的求解结果。
二、ODE78函数源程序1. ODE78函数的源程序如下所示:function [T,Y] = ode78(odefun,tspan,y0,options)t0 = tspan(1); tf = tspan(length(tspan));hmax=0.1; tol=1.e-3;if nargin < 4 options=[]; endif ~isempty(options)if isfield(options,'hmax') hmax=options.hmax; endif isfield(options,'tol') tol=options.tol; endendh=0.1; y=y0(:); t = t0; T = t; Y = y'; iflag=1;m本人n loopwhile iflag > 0h0=h;if t+h > tfh = tf - t;endk1=feval(odefun,t,y);k2=feval(odefun,t+h/7,y+(h/7)*k1);k3=feval(odefun,t+h/6,y+(h/6)*k1+(h/6)*k2);k4=feval(odefun,t+(5*h/12),y+(5*h/12)*k1-(25*h/16)*k2+(25*h/16)*k3);k5=feval(odefun,t+(3*h/4),y+(3*h/4)*k1 +(3*h/4)*k2-(3*h/4)*k3+(9*h/4)*k4); k6=feval(odefun,t+h,y+(h)*k1-(3*h)*k2+(12*h)*k3-(12*h)*k4+(8*h)*k5);k2p=feval(odefun,t+(7*h/8),y+(7*h/8)*k1 + (7*h/8)*k2 -(21*h/8)*k3 +(35*h/8)*k4 -(35*h/8)*k5 +(7*h/8)*k6);k5p=feval(odefun,t+(3*h/4),y+(3*h/4)*k1 +(3*h/4)*k2-(3*h/4)*k3+(9*h/4)*k4 +(9*h/4)*k5-(3*h/4)*k6);ynew=y+h*(k1/11+28*k3/54+31*k4/54-2*k5/27); k5=k5-k5p; y5=tol/max(abs(y),ynew.*tol)+max(abs(y),ynew.*tol);if max(y5)-1<= .OKy=ynew; t=t+h; T=[T t]; Y=[Y; y']; endend2. 上文是ODE78函数的源程序,其中包括了主要的计算过程和参数设置。
Matlab实现的简易计算器程序代码
function varargout = jisuanqi(varargin)% JISUANQI M-file for jisuanqi.fig% JISUANQI, by itself, creates a new JISUANQI or raises the existing % singleton*.%% H = JISUANQI returns the handle to a new JISUANQI or the handle to % the existing singleton*.%% JISUANQI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in JISUANQI.M with the given input arguments. %% JISUANQI('Property','Value',...) creates a new JISUANQI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before jisuanqi_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application % stop. All inputs are passed to jisuanqi_OpeningFcn via varargin. %% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help jisuanqi% Last Modified by GUIDE v2.5 20-Jul-2011 09:45:20% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...'gui_OutputFcn', @jisuanqi_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before jisuanqi is made visible.function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin) handles.current_str='';%´ËΪ´æ´¢µ±Ç°µÄ×Ö·û´®handles.L1_str='';handles.L2_str='';%´ËΪ´æ´¢ÉÏÒ»¸öÊý×ÖµÄ×Ö·û´®% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to jisuanqi (see VARARGIN)% Choose default command line output for jisuanqihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jisuanqi wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = jisuanqi_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in Number_0.function Number_0_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ0£¬Èç¹û°´ÁË0¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'0');handles.L1_str=strcat(handles.L1_str,'0');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_0 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in xiaoshudian.function xiaoshudian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'.');guidata(hObject, handles);%СÊýµã% hObject handle to xiaoshudian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jia_jian.function jia_jian_Callback(hObject, eventdata, handles)% hObject handle to jia_jian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_1.function Number_1_Callback(hObject, eventdata, handles)% hObject handle to Number_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ1£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'1');handles.L1_str=strcat(handles.L1_str,'1');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% --- Executes on button press in Number_2.function Number_2_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ2£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'2');handles.L1_str=strcat(handles.L1_str,'2');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_3.function Number_3_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ3£¬Èç¹û°´ÁË3¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'3');handles.L1_str=strcat(handles.L1_str,'3');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_4.function Number_4_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ4£¬Èç¹û°´ÁË4¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'4');handles.L1_str=strcat(handles.L1_str,'4');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_5.function Number_5_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ5£¬Èç¹û°´ÁË5¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'5');handles.L1_str=strcat(handles.L1_str,'5');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_6.function Number_6_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ6£¬Èç¹û°´ÁË6¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'6');handles.L1_str=strcat(handles.L1_str,'6');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_7.function Number_7_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ7£¬Èç¹û°´ÁË7¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'7');handles.L1_str=strcat(handles.L1_str,'7');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_8.function Number_8_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ8£¬Èç¹û°´ÁË8¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'8');handles.L1_str=strcat(handles.L1_str,'8');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_9.function Number_9_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ9£¬Èç¹û°´ÁË9¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'9');handles.L1_str=strcat(handles.L1_str,'9');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jiahao.function jiahao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'+');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jiahao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in denghao.function denghao_Callback(hObject, eventdata, handles)st = get(handles.edit1,'String');%¼ÆËã½á¹û£¬²¢´æ·Åµ½µÚ¶þ¸ö±à¼-¿òÖС£val = eval(st);s = num2str(val);set(handles.edit2,'String',s);guidata(hObject, handles);% hObject handle to denghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jianhao.function jianhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'-');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jianhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in X_daoshu.function X_daoshu_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'\1');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_daoshu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in chenghao.function chenghao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'*');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chenghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in baifenhao.function baifenhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/100');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);handles.L2_str=handles.L1_str;handles.L1_str = '';% hObject handle to baifenhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in chuhao.function chuhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chuhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Sqrt.function Sqrt_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sqrt');handles.L1_str=strcat(handles.L1_str,'sqrt');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Sqrt (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in X_lifang.function X_lifang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^3');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_lifang (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in x_pingfang.function x_pingfang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^2');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to x_pingfang (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in zhengqie.function zhengqie_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'tan');handles.L1_str=strcat(handles.L1_str,'tan');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengqie (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in zhengxian.function zhengxian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sin');handles.L1_str=strcat(handles.L1_str,'sin');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in CE.function CE_Callback(hObject, eventdata, handles)% hObject handle to CE (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in C.function C_Callback(hObject, eventdata, handles)% hObject handle to C (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.current_str = '';set(handles.edit1,'String','0.');set(handles.edit2,'String','0.');guidata(hObject, handles);% --- Executes on button press in Backspace.function Backspace_Callback(hObject, eventdata, handles)% hObject handle to Backspace (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)s1 = handles.current_str;handles.current_str = s1(1:length(s1)-1);set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endif ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')) ;end% --- Executes on button press in shujujiazai.function shujujiazai_Callback(hObject, eventdata, handles)% hObject handle to shujujiazai (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global YSData;[filename,pathname,FILTERINDEX]=uigetfile({'*.xls';'*.dat';'*.*'},'Ñ¡ÔñÊý¾ÝÎļþ');if(FILTERINDEX==0)return;endstr_filename=[pathname,filename];fid=fopen(str_filename,'rt');if(fid==-1)errordlg('Open file error!','Open error');return;endYSData=xlsread(str_filename);set(handles.lujingxianshi,'String',str_filename);set(handles.tuxingxianshi,'Enable','on');function lujingxianshi_Callback(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.lujingxianshi,'String',pathname);% Hints: get(hObject,'String') returns contents of lujingxianshi as text % str2double(get(hObject,'String')) returns contents of lujingxianshi as a double% --- Executes during object creation, after setting all properties. function lujingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in zhuxingtu.function zhuxingtu_Callback(hObject, eventdata, handles)% hObject handle to zhuxingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',1);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhuxingtu% --- Executes on button press in zhexiantu.function zhexiantu_Callback(hObject, eventdata, handles)% hObject handle to zhexiantu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',1);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhexiantu% --- Executes on button press in yuanbingtu.function yuanbingtu_Callback(hObject, eventdata, handles)% hObject handle to yuanbingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',1);% Hint: get(hObject,'Value') returns toggle state of yuanbingtu% --- Executes on button press in tuxingxianshi.function tuxingxianshi_Callback(hObject, eventdata, handles)global YSData;%Åжϡ®Í¼ÏñÀàÐÍ¡¯ÏÂ×éºÏ¿òÖеĸ÷µ¥Ñ¡°´Å¥µÄÑ¡ÖÐÇé¿ö£¬²¢´æ´¢ÔÚIndex_rad iobuttonÖÐIndex_radiobutton=get([handles.zhuxingtu,handles.zhexiantu,handles.yuan bingtu],'Value');%½øÐÐͼÐÎÉú³Éif(Index_radiobutton{1}==1)bar(YSData);title('¸÷Êý¾ÝÖ±·½Í¼');endif(Index_radiobutton{2}==1)plot(YSData);title('¸÷Êý¾ÝÕÛÏßͼ');endif(Index_radiobutton{3}==1)pie(YSData);title('¸÷Êý¾ÝÔ²±ýͼ');end% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% global YSData;% --- Executes during object creation, after setting all properties. function tuxingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called function edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit2 as text% str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in Quit.function Quit_Callback(hObject, eventdata, handles)% hObject handle to Quit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close(gcf);% --- Executes during object deletion, before destroying properties. function zhengxian_DeleteFcn(hObject, eventdata, handles)% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end。
matlab用于计算方法的源程序
matlab用于计算方法的源程序1、newdon迭代法求解非线性方程function[xkt]=newdontoequation(f,df,x0,eps)%牛顿迭代法解线性方程%[xkt]=newdontoequation(f,df,x0,eps)%x:对数求解%k:运算次数%t:运算时间%f:原函数,定义为内联函数?:函数的倒数,定义为内联函数%x0:初始值%eps:误差限% %应用领域举例:%f=inline('x^3+4*x^2-10');?=inline('3*x^2+8*x');%x=newdontoequation(f,df,1,0.5e-6)%[xk]=newdontoequation(f,df,1,0.5e-6)%[xkt]=newdontoequation(f,df,1,0.5e-6)%函数的最后一个参数也可以不写。
默认情况下,eps=0.5e-6%[xkt]=newdontoequation(f,df,1)ifnargin==3eps=\endtic;k=0;while1x=\k=\ifabs(x-x0)<eps||k>30break;endx0=x;endt=toc;ifk>=30disp('运算次数太多。
');x=\t=\end2、newdon迭代法求解非线性方程组functiony=\%牛顿迭代法解非线性方程组的测试函数%定义是必须定义为列向量y(1,1)=x(1).^2-10*x(1)+x(2).^2+8;y(2,1)=x(1).*x(2).^2+x(1)-10*x(2)+8;return;functiony=\%牛顿迭代法解非线性方程组的测试函数的导数y(1,1)=2*x(1)-10;y(1,2)=2*x(2);y(2,1)=x(2).^+1;y(2,2)=2*x(1).*x(2)-10;return;以上两个函数仅供下面程序的测试function[xkt]=newdontoequations(f,df,x0,eps)%牛顿迭代法解非线性方程组%[xkt]=newdontoequations(f,df,x0,eps)%x:近似解%k:迭代次数%t:运算时间%f:方程组(事先定义)?:方程组的导数(事先定义)%x0:初始值%eps:误差减半%%说明:由于虚参f和df的类型都是函数,使用前需要事先在当前目录下采用函数m 文件定义%另外在使用此函数求解非线性方程组时,需要在函数名前加符号“@”,如下所示%%应用领域举例:%x0=[0,0];eps=0.5e-6;%x=newdontoequations(@newdonf,@newdondf,x0,eps)%[xk]=newdontoequations(@ne wdonf,@newdondf,x0,eps)%[xkt]=newdontoequations(@newdonf,@newdondf,x0,eps)%函数的最后一个参数也可以不写下。
一些简单的算法MATLAB代码
一些简单的算法MATLAB代码1.冒泡排序算法```matlabfunction sortedArray = bubbleSort(array)n = length(array);for i = 1:n-1for j = 1:n-iif array(j) > array(j+1)temp = array(j);array(j) = array(j+1);array(j+1) = temp;endendendsortedArray = array;end```2.选择排序算法```matlabfunction sortedArray = selectionSort(array) n = length(array);for i = 1:n-1minIndex = i;for j = i+1:nif array(j) < array(minIndex)minIndex = j;endendif minIndex ~= itemp = array(i);array(i) = array(minIndex);array(minIndex) = temp;endendsortedArray = array;end```3.插入排序算法```matlabfunction sortedArray = insertionSort(array) n = length(array);for i = 2:nkey = array(i);j=i-1;while j >= 1 && array(j) > keyarray(j+1) = array(j);j=j-1;endarray(j+1) = key;endsortedArray = array;end```4.快速排序算法```matlabfunction sortedArray = quickSort(array)n = length(array);if n <= 1sortedArray = array;return;endpivot = array(1);smaller = [];larger = [];for i = 2:nif array(i) <= pivotsmaller = [smaller array(i)];elselarger = [larger array(i)];endendsortedArray = [quickSort(smaller) pivot quickSort(larger)]; end```5.阶乘算法```matlabfunction factorial = calculateFactorial(n)if n == 0factorial = 1;elsefactorial = n * calculateFactorial(n-1);endend```6.斐波那契数列算法```matlabfunction fibonacci = generateFibonacci(n) fibonacci = zeros(n, 1);fibonacci(1) = 0;fibonacci(2) = 1;for i = 3:nfibonacci(i) = fibonacci(i-1) + fibonacci(i-2); endend```7.素数判断算法```matlabfunction isPrime = checkPrime(n) isPrime = true;if n <= 1isPrime = false;return;endfor i = 2:sqrt(n)if rem(n, i) == 0isPrime = false;break;endendend```8.最大公约数算法(辗转相除法)```matlabfunction gcd = calculateGCD(a, b)if b == 0gcd = a;elsegcd = calculateGCD(b, rem(a, b));endend```9.最小公倍数算法```matlabfunction lcm = calculateLCM(a, b)lcm = a * b / calculateGCD(a, b);end```10.线性算法```matlabfunction index = linearSearch(array, target) n = length(array);index = -1;for i = 1:nif array(i) == targetindex = i;break;endendend```这些简单的算法代码可以用于排序、数字计算和等问题。
数值计算实例MATLAB实现(附带详细源码)
数值计算实例MATLAB实现附带详细源码1.在化学反应中,A 的一个分子和 B 的一个分子结合形成物质 C 的分子。
若在时刻t 时,物质 C 的浓度为() y t ,则其是下述初值问题的解()()() ,00y k a y b y y '=--=其中k 为正常数,a 和 b 分别表示 A 和 B 的初始浓度。
假设k = 0.01, a =70毫摩/升, b = 50 毫摩/升. 该方程的真解为0.20.2350(1)()75t te y t e---=- (1)自己编写程序,使用四阶经典Runge-Kutta (龙格-库塔法),以步长为0.5h =,在区间[0, 20]上给出() y t 的近似解; (2)列表给出真解和近似解的比较;(3)讨论当t →∞时,近似解的变化趋势,并分析该数值结果。
解:数学原理:四阶经典Runge-Kutta (龙格-库塔法)112341213243(22)6(,)(,)22(,)22(,)m m m m m m m m m m hu u k k k k k f t u h hk f t u k h hk f t u k k f t h u hk +=++++==++=++=++程序设计见附录 结果如下表:(3)近似解变化趋势当t→∞时,由以下极限方程可知:0.20.2350(1)()75lim()tttey tey t--→∞⎧-=-⎪⎨⎪⎩随着t→∞,近似值越来越接近真实值,极限的真实值为50,lim()50ty t→∞=,变化趋势也可由一下曲线图表示:感想:四阶Runge-Kutta法计算的结果精度非常好,其结果与真实解误差不大。
2.考虑定义在闭区间[−5, 5]上的函数()2112()5f x x -=+ ;(1)利用等距节点构造次数分别为 n = 4,8,16, 32 的插值多项式()n p x ,并分别画()()()()481632,,,p x p x p x p x ;(2)利用chebyshev 零点构造次数分别为 n = 4,8,16, 32 的插值多项式()n pp x()()()()481632,,,pp x pp x pp x pp x ;(3)画出当 n = 32 时,两种插值多项式的比较图,误差图,并给出相应的误差估计;(4)在这个问题中能观察到龙格现象吗? 解:数学原理:拉格朗日插值多项式:001122()()()()()n n n L x l x y l x y l x y l x y =+++011011()()()()(),0,1,2,()()()()k k n k k k k k k k n x x x x x x x x l x k n x x x x x x x x -+-+----==----0()()()nn n in k k k k k j k jj kx x L x l x y y x x ===≠-==-∑∑∏程序设计见附录(1) 利用等距节点构造次数分别为 n = 4,8,16, 32 的插值多项式如下: ()43240.00160.00.0640.60061400p x x x x x ++=++()876542830.00280.00640.02500.02500.00640.00260.000168.001p x x x x x x x x x ++++++=++()1615141312161110987654320.00210.00280.00410.0064 60.01120.02500.09290.09290.02050 0.01120.00640.00410.002.00160180.021.000p x x x x x x x x x x x x x x x x x ++++++++++++++=++()3231302928272632252423222120191817160001600018000210002400028000340004100050006400083001120016100250004350092902906029p x .x .x .x .x .x .x .x .x .x .x .x .x .x .x .x .x x .=+++++++++++++++++151413121110987654320600929004350025000161001120008300064000500041000340002800024000210001800016x .x .x .x .x .x .x .x .x .x .x .x .x .x .x .+++++++++++++++(2)利用chebyshev 零点构造次数分别为 n = 4,8,16, 32 的插值多项式如下:()43240.00160.00320.00320.0016x x p x x p x =++++()87654328+0.00190.00320.01080.01080.00320.00196=0.0.0106001pp x x x x x x x x x +++++++()161514131211109168765432=0.0016 0.0017 0.0019 0.00230.00320.00520.01080.0403 1.00000.04030.01080.00520.00320.00230.0019 0.0017 0.0016 pp x x x x x x x x x x x x x x x x x ++++++++++++++++()323130292827263225242322212019181700016000160001700017000190002100023000270003200040000520007100108001860040301428pp x .x .x .x .x .x .x .x .x .x .x .x .x .x .x .x .x x =+++++++++++++++++16151413121110987654320142800403001860010800071000520004000320002700023000210001900017000170001600016.x .x .x .x .x .x .x .x .x .x .x .x .x .x .x .+++++++++++++++++(3)两种插值多项式的比较误差图如下(a)等距插值误差 (b) chebyshev零点插值误差(4) 等距插值在高次插值中能观察到龙格现象,而chebyshev零点插值观察不到龙格现象。
(完整版)潮流计算的MATLAB源程序
%简单潮流计算的牛顿拉夫逊程序,相关的原始数据数据数据输入格式如下:%B1是支路参数矩阵,第一列和第二列是节点编号。
节点编号由小到大编写%对于含有变压器的支路,第一列为低压侧节点编号,第二列为高压侧节点%编号,将变压器的串联阻抗置于低压侧处理。
%第三列为支路的串列阻抗参数。
%第四列为支路的对地导纳参数。
%第五列为含变压器支路的变压器的变比%第六列为变压器是否含有变压器的参数,其中“1"为含有变压器,%“0”为不含有变压器。
%B2为节点参数矩阵,其中第一列为节点注入发电功率参数;第二列为节点负荷功率参数;第三列为节点电压参数;第六列为节点类型参数,其中“1”为平衡节点,“2”为PQ节点,“3”为PV节点参数。
%X为节点号和对地参数矩阵.其中第一列为节点编号,第二列为节点对地%参数。
n=input(’请输入节点数:n=');n1=input(’请输入支路数:n1=');isb=input(’请输入平衡节点号:isb=');pr=input('请输入误差精度:pr=');B1=input(’请输入支路参数:B1=’);B2=input(’请输入节点参数:B2=’);X=input('节点号和对地参数:X=');Y=zeros(n);Times=1; %置迭代次数为初始值%创建节点导纳矩阵for i=1:n1if B1(i,6)==0 %不含变压器的支路p=B1(i,1);q=B1(i,2);Y(p,q)=Y(p,q)-1/B1(i,3);Y(q,p)=Y(p,q);Y(p,p)=Y(p,p)+1/B1(i,3)+0。
5*B1(i,4);Y(q,q)=Y(q,q)+1/B1(i,3)+0。
5*B1(i,4);else %含有变压器的支路p=B1(i,1);q=B1(i,2);Y(p,q)=Y(p,q)—1/(B1(i,3)*B1(i,5));Y(q,p)=Y(p,q);Y(p,p)=Y(p,p)+1/B1(i,3);Y(q,q)=Y(q,q)+1/(B1(i,5)^2*B1(i,3));endendYOrgS=zeros(2*n—2,1);DetaS=zeros(2*n—2,1); %将OrgS、DetaS初始化%创建OrgS,用于存储初始功率参数h=0;j=0;for i=1:n %对PQ节点的处理if i~=isb&B2(i,6)==2h=h+1;for j=1:nOrgS(2*h-1,1)=OrgS(2*h—1,1)+real(B2(i,3))*(real(Y (i,j))*real(B2(j,3))-imag(Y(i,j))*imag(B2(j,3)))+imag(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));OrgS(2*h,1)=OrgS(2*h,1)+imag(B2(i,3))*(real(Y(i,j))*real(B2(j,3))—imag(Y(i,j))*imag(B2(j,3)))-real(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));endendendfor i=1:n %对PV节点的处理,注意这时不可再将h初始化为0 if i~=isb&B2(i,6)==3h=h+1;for j=1:nOrgS(2*h—1,1)=OrgS(2*h—1,1)+real(B2(i,3))*(real(Y(i,j))*real(B2(j,3))-imag(Y(i,j))*imag(B2(j,3)))+imag(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));OrgS(2*h,1)=OrgS(2*h,1)+imag(B2(i,3))*(real(Y(i,j))*real(B2(j,3))-imag(Y(i,j))*imag(B2(j,3)))—real(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));endendendOrgS%创建PVU 用于存储PV节点的初始电压PVU=zeros(n-h—1,1);t=0;for i=1:nif B2(i,6)==3t=t+1;PVU(t,1)=B2(i,3);endendPVU%创建DetaS,用于存储有功功率、无功功率和电压幅值的不平衡量h=0;for i=1:n %对PQ节点的处理if i~=isb&B2(i,6)==2h=h+1;DetaS(2*h—1,1)=real(B2(i,2))-OrgS(2*h—1,1);DetaS(2*h,1)=imag(B2(i,2))—OrgS(2*h,1);endendt=0;for i=1:n %对PV节点的处理,注意这时不可再将h初始化为0 if i~=isb&B2(i,6)==3h=h+1;t=t+1;DetaS(2*h-1,1)=real(B2(i,2))-OrgS(2*h-1,1);DetaS(2*h,1)=real(PVU(t,1))^2+imag(PVU(t,1))^2-real(B2(i,3))^2—imag(B2(i,3))^2;endendDetaS%创建I,用于存储节点电流参数i=zeros(n—1,1);h=0;for i=1:nif i~=isbh=h+1;I(h,1)=(OrgS(2*h—1,1)-OrgS(2*h,1)*sqrt(-1))/conj(B2(i,3));endendI%创建Jacbi(雅可比矩阵)Jacbi=zeros(2*n—2);h=0;k=0;for i=1:n %对PQ节点的处理if B2(i,6)==2h=h+1;for j=1:nif j~=isbk=k+1;if i==j %对角元素的处理Jacbi(2*h-1,2*k—1)=—imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3))+imag(I(h,1));Jacbi(2*h-1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3))+real(I(h,1));Jacbi(2*h,2*k-1)=-Jacbi(2*h-1,2*k)+2*real (I(h,1));Jacbi(2*h,2*k)=Jacbi(2*h-1,2*k-1)—2*imag (I(h,1));else %非对角元素的处理Jacbi(2*h—1,2*k-1)=—imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3));Jacbi(2*h—1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3));Jacbi(2*h,2*k-1)=—Jacbi(2*h—1,2*k);Jacbi(2*h,2*k)=Jacbi(2*h—1,2*k—1);endif k==(n-1) %将用于内循环的指针置于初始值,以确保雅可比矩阵换行k=0;endendendendendk=0;for i=1:n %对PV节点的处理if B2(i,6)==3h=h+1;for j=1:nif j~=isbk=k+1;if i==j %对角元素的处理Jacbi(2*h—1,2*k-1)=—imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3))+imag(I(h,1));Jacbi(2*h—1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3))+real(I(h,1));Jacbi(2*h,2*k-1)=2*imag(B2(i,3));Jacbi(2*h,2*k)=2*real(B2(i,3));else %非对角元素的处理Jacbi(2*h-1,2*k—1)=-imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3));Jacbi(2*h—1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3));Jacbi(2*h,2*k—1)=0;Jacbi(2*h,2*k)=0;endif k==(n-1) %将用于内循环的指针置于初始值,以确保雅可比矩阵换行k=0;endendendendendJacbi%求解修正方程,获取节点电压的不平衡量DetaU=zeros(2*n—2,1);DetaU=inv(Jacbi)*DetaS;DetaU%修正节点电压j=0;for i=1:n %对PQ节点处理if B2(i,6)==2j=j+1;B2(i,3)=B2(i,3)+DetaU(2*j,1)+DetaU(2*j—1,1)*sqrt(—1);endendfor i=1:n %对PV节点的处理if B2(i,6)==3j=j+1;B2(i,3)=B2(i,3)+DetaU(2*j,1)+DetaU(2*j-1,1)*sqrt(-1);endendB2%开始循环**********************************************************************while abs(max(DetaU))>prOrgS=zeros(2*n-2,1); %初始功率参数在迭代过程中是不累加的,所以在这里必须将其初始化为零矩阵h=0;j=0;for i=1:nif i~=isb&B2(i,6)==2h=h+1;for j=1:nOrgS(2*h—1,1)=OrgS(2*h-1,1)+real(B2(i,3))*(real(Y(i,j))*real(B2(j,3))—imag(Y(i,j))*imag(B2(j,3)))+imag(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));OrgS(2*h,1)=OrgS(2*h,1)+imag(B2(i,3))*(real(Y(i,j))*real(B2(j,3))—imag(Y(i,j))*imag(B2(j,3)))-real(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));endendendfor i=1:nif i~=isb&B2(i,6)==3h=h+1;for j=1:nOrgS(2*h-1,1)=OrgS(2*h-1,1)+real(B2(i,3))*(real(Y(i,j))*real(B2(j,3))-imag(Y(i,j))*imag(B2(j,3)))+imag(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));OrgS(2*h,1)=OrgS(2*h,1)+imag(B2(i,3))*(real(Y(i,j))*real (B2(j,3))-imag(Y(i,j))*imag(B2(j,3)))—real(B2(i,3))*(real(Y(i,j))*imag(B2(j,3))+imag(Y(i,j))*real(B2(j,3)));endendendOrgS%创建DetaSh=0;for i=1:nif i~=isb&B2(i,6)==2h=h+1;DetaS(2*h-1,1)=real(B2(i,2))—OrgS(2*h—1,1);DetaS(2*h,1)=imag(B2(i,2))—OrgS(2*h,1);endendt=0;for i=1:nif i~=isb&B2(i,6)==3h=h+1;t=t+1;DetaS(2*h—1,1)=real(B2(i,2))—OrgS(2*h—1,1);DetaS(2*h,1)=real(PVU(t,1))^2+imag(PVU(t,1))^2-real(B2(i,3))^2-imag(B2(i,3))^2;endendDetaS%创建Ii=zeros(n-1,1);h=0;for i=1:nif i~=isbh=h+1;I(h,1)=(OrgS(2*h—1,1)-OrgS(2*h,1)*sqrt(—1))/conj(B2(i,3));endendI%创建JacbiJacbi=zeros(2*n—2);h=0;k=0;for i=1:nif B2(i,6)==2h=h+1;for j=1:nif j~=isbk=k+1;if i==jJacbi(2*h—1,2*k—1)=-imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3))+imag(I(h,1));Jacbi(2*h-1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3))+real(I(h,1));Jacbi(2*h,2*k—1)=-Jacbi(2*h-1,2*k)+2*real(I(h,1));Jacbi(2*h,2*k)=Jacbi(2*h-1,2*k—1)—2*imag(I(h,1));elseJacbi(2*h—1,2*k—1)=-imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3));Jacbi(2*h—1,2*k)=real(Y(i,j))*real (B2(i,3))+imag(Y(i,j))*imag(B2(i,3));Jacbi(2*h,2*k—1)=-Jacbi(2*h-1,2*k);Jacbi(2*h,2*k)=Jacbi(2*h—1,2*k-1);endif k==(n-1)k=0;endendendendendk=0;for i=1:nif B2(i,6)==3h=h+1;for j=1:nif j~=isbk=k+1;if i==jJacbi(2*h—1,2*k-1)=—imag(Y(i,j))*real(B2(i,3))+real(Y(i,j))*imag(B2(i,3))+imag(I(h,1));Jacbi(2*h-1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3))+real(I(h,1));Jacbi(2*h,2*k-1)=2*imag(B2(i,3));Jacbi(2*h,2*k)=2*real(B2(i,3));elseJacbi(2*h-1,2*k—1)=-imag(Y(i,j))*real (B2(i,3))+real(Y(i,j))*imag(B2(i,3));Jacbi(2*h-1,2*k)=real(Y(i,j))*real(B2(i,3))+imag(Y(i,j))*imag(B2(i,3));Jacbi(2*h,2*k-1)=0;Jacbi(2*h,2*k)=0;endif k==(n—1)k=0;endendendendendJacbiDetaU=zeros(2*n-2,1);DetaU=inv(Jacbi)*DetaS;DetaU%修正节点电压j=0;for i=1:nif B2(i,6)==2j=j+1;B2(i,3)=B2(i,3)+DetaU(2*j,1)+DetaU(2*j—1,1)*sqrt(-1);endendfor i=1:nif B2(i,6)==3j=j+1;B2(i,3)=B2(i,3)+DetaU(2*j,1)+DetaU(2*j-1,1)*sqrt(—1);endendB2Times=Times+1; %迭代次数加1endTimes一个原始数据的例子节点数 5支路数 5平衡节点编号 5精度pr 0。
matlab国赛算法源代码
matlab国赛算法源代码国赛算法源代码主要包括各种数学建模常用算法的实现,如评价预测、优化等。
在这些算法中,有一些经典的算法在MATLAB中已有现成的源代码。
以下是一些建议您关注的MATLAB国赛算法源代码:1. 线性规划:MATLAB中有现成的线性规划求解器,可以使用`linprog`函数进行求解。
2. 非线性规划:MATLAB中的优化工具箱提供了非线性规划的求解方法,如梯度下降、牛顿法等。
3. 遗传算法:MATLAB的遗传算法工具箱(GA)提供了遗传算法的实现,可以用于解决优化问题、信号处理、图像处理等问题。
4. 神经网络:MATLAB中的神经网络工具箱(NN)提供了神经网络的构建、训练和仿真功能,可以用于分类、预测等任务。
5. 支持向量机:MATLAB中的统计学习工具箱(SLS)提供了支持向量机(SVM)的实现,可以用于分类和回归任务。
6. 决策树:MATLAB中的统计学习工具箱还提供了决策树的实现,可以用于分类和回归任务。
7. 聚类算法:MATLAB中的聚类算法包括K均值聚类、高斯混合模型等,可以用于无监督学习任务。
8. 时间序列分析:MATLAB中的时间序列分析工具箱(TS)提供了多种时间序列分析方法,如ARIMA模型、状态空间模型等。
9. 图像处理:MATLAB中的图像处理工具箱(IP)提供了丰富的图像处理算法,如滤波、特征提取、目标检测等。
10. 网络优化:MATLAB中的网络优化工具箱(NW)提供了网络优化方法的实现,如最短路径算法、最大流最小割算法等。
以上仅为部分国赛算法源代码的示例,实际应用中,您可能需要根据具体问题选择合适的算法并进行相应的调整。
在备战国赛过程中,可以通过学习这些经典算法的源代码,了解其原理和实现,从而提高自己的数学建模和编程能力。
同时,也可以参考一些国内外优秀的数学建模竞赛论文,学习他们的方法和思路。
最后,祝您在比赛中取得好成绩!。
数值分析数学实验上机实验编程matlab源代码
Newton法及改进的Newton法源程序:clear%%%% 输入函数f=input('请输入需要求解函数>>','s')%%%求解f(x)的导数df=diff(f);%%%改进常数或重根数miu=2;%%%初始值x0x0=input('input initial value x0>>');k=0;%迭代次数max=100;%最大迭代次数R=eval(subs(f,'x0','x'));%求解f(x0),以确定初值x0时否就是解while (abs(R)>1e-8)x1=x0-miu*eval(subs(f,'x0','x'))/eval(subs(df,'x0','x'));R=x1-x0;x0=x1;k=k+1;if (eval(subs(f,'x0','x'))<1e-10);breakendif k>max;%如果迭代次数大于给定值,认为迭代不收敛,重新输入初值ss=input('maybe result is error,choose a new x0,y/n?>>','s');ifstrcmp(ss,'y')x0=input('input initial value x0>>');k=0; elsebreakendendendk;%给出迭代次数x=x0;%给出解Gauss消元法源程序:cleara=input('输入系数阵:>>\n')b=input('输入列阵b:>>\n')n=length(b);A=[a b]x=zeros(n,1);%%%函数主体Yipusilong-1;for k=1:n-1;%%%是否进行主元选取if abs(A(k,k))<yipusilong;%事先给定的认为有必要选主元的小数yzhuyuan=1;elseyzhuyuan=0;endifyzhuyuan;%%%%选主元t=A(k,k);for r=k+1:n;if abs(A(r,k))>abs(t)p=r;else p=k;endend%%%交换元素if p~=k; for q=k:n+1;s=A(k,q);A(k,q)=A(p,q);A(p,q)=s;endendend%%%判断系数矩阵是否奇异或病态非常严重if abs(A(k,k))<yipusilongdisp(‘矩阵奇异,解可能不正确’)end%%%%计算消元,得三角阵for r=k+1:n;m=A(r,k)/A(k,k);for q=k:n+1;A(r,q)=A(r,q)-A(k,q)*m;endendend%%%%求解xx(n)=A(n,n+1)/A(n,n);for k=n-1:-1:1;s=0;for r=k+1:n;s=s+A(k,r)*x(r);endt=(A(k,n+1)-s) x(k)=(A(k,n+1)-s)/A(k,k) end Lagrange插值源程序:n=input('将区间分为的等份数输入:\n');s=[-1+2/n*[0:n]];%%%给定的定点,Rf为给定的函数x=-1:0.01:1;f=0;for q=1:n+1;l=1;%求插值基函数for k=1:n+1;if k~=q;l=l.*(x-s(k))./(s(q)-s(k));elsel=l;endendf=f+Rf(s(q))*l;%求插值函数endplot(x,f,'r')%作出插值函数曲线grid onhold on分段线性插值源程序clearn=input('将区间分为的等份数输入:\n');s=[-1+2/n*[0:n]];%%%给定的定点,Rf为给定的函数m=0;hh=0.001;for x=-1:hh:1;ff=0;for k=1:n+1;%%%求插值基函数switch kcase 1if x<=s(2);l=(x-s(2))./(s(1)-s(2));elsel=0;endcase n+1if x>s(n);l=(x-s(n))./(s(n+1)-s(n)); elsel=0;endotherwiseif x>=s(k-1)&x<=s(k);l=(x-s(k-1))./(s(k)-s(k-1)); else if x>=s(k)&x<=s(k+1);l=(x-s(k+1))./(s(k)-s(k+1)); elsel=0;endendendff=ff+Rf(s(k))*l;%%求插值函数值endm=m+1;f(m)=ff;end%%%作出曲线x=-1:hh:1;plot(x,f,'r');grid onhold on三次样条插值源程序:(采用第一边界条件)clearn=input('将区间分为的等份数输入:\n');%%%插值区间a=-1;b=1;hh=0.001;%画图的步长s=[a+(b-a)/n*[0:n]];%%%给定的定点,Rf为给定的函数%%%%第一边界条件Rf"(-1),Rf"(1)v=5000*1/(1+25*a*a)^3-50/(1+25*a*a)^4;for k=1:n;%取出节点间距h(k)=s(k+1)-s(k);endfor k=1:n-1;%求出系数向量lamuda,miula(k)=h(k+1)/(h(k+1)+h(k));miu(k)=1-la(k);end%%%%赋值系数矩阵Afor k=1:n-1;for p=1:n-1;switch pcase kA(k,p)=2;case k-1A(k,p)=miu(p+1);case k+1A(k,p)=la(p-1);otherwiseA(k,p)=0;endendend%%%%求出d阵for k=1:n-1;switch kcase 1d(k)=6*f2c([s(k) s(k+1) s(k+2)])-miu(k)*v; case n-1d(k)=6*f2c([s(k) s(k+1) s(k+2)])-la(k)*v; otherwised(k)=6*f2c([s(k) s(k+1) s(k+2)]);endend %%%%求解M阵M=A\d';M=[v;M;v];%%%%m=0;f=0;for x=a:hh:b;if x==a;p=1;elsep=ceil((x-s(1))/((b-a)/n));endff1=0;ff2=0;ff3=0;ff4=0;m=m+1;ff1=1/h(p)*(s(p+1)-x)^3*M(p)/6;ff2=1/h(p)*(x-s(p))^3*M(p+1)/6;ff3=((Rf(s(p+1))-Rf(s(p)))/h(p)-h(p)*(M(p+1)-M(p))/6)*(x-s(p));ff4=Rf(s(p))-M(p)*h(p)*h(p)/6;f(m)=ff1+ff2+ff3+ff4 ;end%%%作出插值图形x=a:hh:b;plot(x,f,'k')hold on grid on 多项式最小二乘法源程序clear%%%给定测量数据点(s,f)s=[3 4 5 6 7 8 9];f=[2.01 2.98 3.50 5.02 5.47 6.02 7.05];%%%计算给定的数据点的数目n=length(f);%%%给定需要拟合的数据的最高次多项式的次数m=10;%%%程序主体for k=0:m;g=zeros(1,m+1);for j=0:m;t=0;for i=1:n;%计算内积(fai(si),fai(si))t=t+fai(s(i),j)*fai(s(i),k);endg(j+1)=t;endA(k+1,:)=g;%法方程的系数矩阵t=0;for i=1:n;%计算内积(f(si),fai(si))t=t+f(i)*fai(s(i),k);endb(k+1,1)=t;enda=A\b%求出多项式系数x=[s(1):0.01:s(n)]';y=0;fori=0:m;y=y+a(i+1)*fai(x,i);endplot(x,y)%作出拟合成的多项式的曲线grid onhold onplot(s,f,'rx') %在上图中标记给定的点表中,L10(x)为Lagrang e插值的10次多项式,S10(x),S40(x)分别代表n=10,40的三次样条插值函数,X10(x),X40(x)分别代表n=10,40的线性分段插值函数。
matlab数值计算代码
matlab数值计算代码Matlab是一种强大的数值计算软件,广泛应用于科学研究、工程设计等领域。
在Matlab中,我们可以使用代码来进行各种数值计算,包括数值积分、数值求解方程、数值解微分方程等。
本文将介绍一些常见的数值计算代码,并说明其原理和应用。
一、数值积分数值积分是利用数值方法求解定积分的过程。
在Matlab中,我们可以使用simpson函数或trapz函数进行数值积分计算。
这两个函数分别采用辛普森公式和梯形公式进行数值积分近似。
例如,下面的代码使用simpson函数计算函数f(x)在区间[a,b]上的定积分:```matlaba = 0;b = 1;n = 100;x = linspace(a, b, n);y = f(x);integral = simpson(y, x);```其中,a和b分别是积分区间的上下限,n是划分区间的个数,x是划分后的区间点,y是函数在各个区间点处的函数值,integral是计算得到的定积分值。
二、数值求解方程数值求解方程是指利用数值方法求解方程的近似解。
在Matlab中,我们可以使用fzero函数或fsolve函数进行数值求解方程。
这两个函数采用不同的求解算法,可以用于求解单变量方程或多变量方程。
例如,下面的代码使用fzero函数求解方程f(x)=0:```matlabx0 = 0;x = fzero(@f, x0);```其中,x0是求解初始值,@f是函数句柄,表示要求解的方程。
x 是求解得到的近似解。
三、数值解微分方程数值解微分方程是指利用数值方法求解微分方程的近似解。
在Matlab中,我们可以使用ode45函数或ode23函数进行数值解微分方程。
这两个函数采用不同的数值方法,可以用于求解常微分方程或偏微分方程。
例如,下面的代码使用ode45函数求解常微分方程dy/dx=f(x,y):```matlabx0 = 0;y0 = 1;xspan = [0, 1];[t, y] = ode45(@f, xspan, y0);```其中,x0和y0分别是初始条件,xspan是求解区间,@f是函数句柄,表示要求解的微分方程。
计算方法MATLAB程序样例
1.利用共轭梯度法求解大规模稀疏方程组1.1.算法原理共轭梯度法是把求解线性方程组的问题转化为求解一个与之等价的二次函数极小值的问题。
对于特定的线性方程组来说,选取与之等价的二次函数之后,可以从任意给定的初始点出发,沿一组关于矩阵A 的共轭方向进行线性搜索,在无舍入误差的假定下,最多迭代n次(其中n为矩阵A 的阶数),就可以求得二次函数的极小点,也就求得了线性方程组A x=b的解。
对于特定线性方程组A x=b,其对应的n元二次函数为:f(x)=12x T Ax−b T x其中A为对称正定矩阵,二次函数的梯度为:∇f(x)=Ax+b 通过一系列的数学推导,最终可以得到共轭梯度的计算公式:{d(0)=r(0)=b−Ax(0)αk=r(k)T d(k)d(k)T Ad(k)x(k+1)=x(k)+αk d(k) r(k+1)=b−Ax(k+1)βk=−r(k+1)T Ad(k)d(k)T Ad(k)d(k+1)=r(k+1)+βk d(k)共轭梯度法在形式上具有迭代法的特征,即给定初始向量x(0)后,由迭代格式x(k+1)=x(k)+αk d(k)能够产生迭代序列x(1),x(2),x(3),最终得到满足约束条件的解。
而该方法中关键的两点是,确定上述迭代格式中的搜索方向d(k)和最佳步长αk。
实际上,搜索方向d(k)是关于矩阵A的共轭向量,在迭代中逐步构造之。
步长αk的确定原则是给定迭代点x(k)和搜索方向d(k)后,要求选取非负实数αk使得f(x(k)+αk d(k))达到最小。
因此,由该算法原理可得共轭梯度法的程序流程图如下图所示。
图 1.1共轭梯度法程序流程图1.2.程序使用说明针对相关问题的源程序及注释如下:clc;clear all;format long;n=100;%A的阶数A=zeros(100);A=diag(repmat([1],1,n-1),1)+diag(repmat([1],1,n-1),-1)+diag(repmat([-2],1,n));%构建三对角矩阵b=zeros(n,1);b(1,1)=-1;b(n,1)=-1;x0=zeros(n,1);d0=b-A*x0;r0=b-A*x0;D0=d0;R0=r0;X0=x0;C=norm(A)*norm(inv(A));%共轭梯度法¨for k=1:3*na0=(r0.'*d0)/(d0.'*A*d0);x=x0+a0*d0;r=b-A*x;beta=-(r.'*A*d0)/(d0.'*A*d0);d=r+beta*d0;x0=x;r0=r;d0=d;if norm(r)< 1e-9seigm(k)=norm(r);breakelseseigm(k)=norm(r);endendR=r0;X=x0;%最速下降法¨r0=R0;d0=D0;x0=X0;%读取初始向量初始残差与初始方向for k=1:3*na0=(r0.'*r0)/(r0.'*A*r0);x=x0-a0*r0;r=A*x-b;f=x-x0;x0=x;r0=r;if norm(f)< 1e-9seigm1(k)=norm(r);breakelseseigm1(k)=norm(r);endendR1=r0;X1=x0;m=1:length(seigm);M=1:length(seigm1);figure;plot(m,log(seigm),'r');%误差对数的变化曲线hold on;grid on;plot(M,log(seigm1),'b');hold on;grid on;legend('¹²éîÌݶȷ¨','×îËÙϽµ·¨','fontname','ËÎÌå');运行上述程序可得共轭梯度法和最速下降法误差收敛速度图。
分享用MATLAB实现计算器程序源代码
function varargout = caculator(varargin) gui_Singleton = 1;gui_State = struct( 'gui_Name' , mfilename, ...'gui_Singleton' , gui_Singleton, .'gui_OpeningFcn' , @caculator_OpeningFcn,'gui_OutputFcn' , @caculator_OutputFcn,'gui_LayoutFcn' , [] , ...'gui_Callback' , []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});endfunction caculator_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject;set(handles.edit1, 'string' , '0' );set(handles.edit5, 'string' , '0' );guidata(hObject, handles);function varargout = caculator_OutputFcn(hObject, eventdata, handles)varargout{1} = handles.output;function edit1_Callback(hObject, eventdata, handles)function edit1_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject, 'BackgroundColor' ),get(0, 'defaultUicontrolBackgroundColor' ))set(hObject, 'BackgroundColor' , 'white' );endfunction edit2_Callback(hObject, eventdata, handles)function edit2_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject, 'BackgroundColor' ),get(0, 'defaultUicontrolBackgroundColor' ))set(hObject, 'BackgroundColor' , 'white' );endfunction edit3_Callback(hObject, eventdata, handles)function edit3_CreateFcn(hObject, eventdata, handles)if ispc && isequal(get(hObject, 'BackgroundColor' ),get(0, 'defaultUicontrolBackgroundColor' ))set(hObject, 'BackgroundColor' , 'white' ); end function pushbutton1_Callback(hObject, eventdata, handles)handles.num1=strcat(get(handles.edit1, 'string' ), '+' ); set(handles.edit1, 'string' ,handles.num1); guidata(hObject,handles);function pushbutton2_Callback(hObject, eventdata, handles)handles.num2=strcat(get(handles.edit1, 'string' ), '-' ); set(handles.edit1, 'string' ,handles.num2); guidata(hObject,handles);function pushbutton3_Callback(hObject, eventdata, handles)handles.num3=strcat(get(handles.edit1, 'string' ), '*' ); set(handles.edit1, 'string' ,handles.num3); guidata(hObject,handles);function pushbutton4_Callback(hObject, eventdata, handles)handles.num4=strcat(get(handles.edit1, 'string' ), '/' ); set(handles.edit1, 'string' ,handles.num4); guidata(hObject,handles);function pushbutton_1_Callback(hObject, eventdata, handles)handles.shu1=get(hObject, 'string' ); handles.yuanshu=get(handles.edit1, 'string' ); ifhandles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ; endif handles.yuanshu(1)== '0' ; handles.yuanshu=handles.yuanshu(2:end);endhandles.shu1=strcat(handles.yuanshu,handles.shu1);if length(handles.shu1)<2;elseif (length(handles.shu1)>=2)&&(handles.shu1(end-1)== ')' )&& (handles.shu1(1)== 'l' ) temp=handles.shu1(end); handles.shu1(end)=handles.shu1(end-1);handles.shu1(end-1)=temp; end set(handles.edit1, 'string' ,handles.shu1); guidata(hObject, handles); function pushbutton_2_Callback(hObject, eventdata, handles)handles.shu2=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' ); if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu2=strcat(handles.yuanshu,handles.shu2);if length(handles.shu2)<2elseif (length(handles.shu2)>=2)&&(handles.shu2(end-1)== ')' )&& (handles.shu2(1)== 'l' )temp=handles.shu2(end);handles.shu2(end)=handles.shu2(end-1);handles.shu2(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu2);guidata(hObject, handles);function pushbutton_4_Callback(hObject, eventdata, handles)handles.shu4=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' );if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu4=strcat(handles.yuanshu,handles.shu4);if length(handles.shu4)<2elseif (length(handles.shu4)>=2)&&(handles.shu4(end-1)== ')' )&& (handles.shu4(1)== 'l' ) temp=handles.shu4(end);handles.shu4(end)=handles.shu4(end-1);handles.shu4(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu4);guidata(hObject, handles);function pushbutton_3_Callback(hObject, eventdata, handles)handles.shu3=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' ); if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu3=strcat(handles.yuanshu,handles.shu3);if length(handles.shu3)<2elseif (length(handles.shu3)>=2)&&(handles.shu3(end-1)== ')' )&& (handles.shu3(1)== 'l' ) temp=handles.shu3(end);handles.shu3(end)=handles.shu3(end-1);handles.shu3(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu3);guidata(hObject, handles);function pushbutton_5_Callback(hObject, eventdata, handles)handles.shu5=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' );if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu5=strcat(handles.yuanshu,handles.shu5);if length(handles.shu5)<2elseif (length(handles.shu5)>=2)&&(handles.shu5(end-1)== ')' )&& (handles.shu5(1)== 'l' ) temp=handles.shu5(end);handles.shu5(end)=handles.shu5(end-1);handles.shu5(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu5);guidata(hObject, handles);function pushbutton_9_Callback(hObject, eventdata, handles)handles.shu9=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' );if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu9=strcat(handles.yuanshu,handles.shu9);if length(handles.shu9)<2elseif (length(handles.shu9)>=2)&&(handles.shu9(end-1)== ')' )&& (handles.shu9(1)== 'l' ) temp=handles.shu9(end);handles.shu9(end)=handles.shu9(end-1);handles.shu9(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu9);guidata(hObject, handles);function pushbutton_7_Callback(hObject, eventdata, handles)handles.shu7=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' );if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);end handles.shu7=strcat(handles.yuanshu,handles.shu7);if length(handles.shu7)<2elseif (length(handles.shu7)>=2)&&(handles.shu7(end-1)== ')' )&& (handles.shu7(1)== 'l' ) temp=handles.shu7(end);handles.shu7(end)=handles.shu7(end-1);handles.shu7(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu7);guidata(hObject, handles);function pushbutton_8_Callback(hObject, eventdata, handles)handles.shu8=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' );if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu8=strcat(handles.yuanshu,handles.shu8);if length(handles.shu8)<2elseif (length(handles.shu8)>=2)&&(handles.shu8(end-1)== ')' )&& (handles.shu8(1)== 'l' ) temp=handles.shu8(end);handles.shu8(end)=handles.shu8(end-1);handles.shu8(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu8);guidata(hObject, handles);function pushbutton_6_Callback(hObject, eventdata, handles)handles.shu6=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' );if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ;handles.yuanshu=handles.yuanshu(2:end);endhandles.shu6=strcat(handles.yuanshu,handles.shu6);if length(handles.shu6)<2elseif (length(handles.shu6)>=2)&&(handles.shu6(end-1)== ')' )&& (handles.shu6(1)== 'l' )temp=handles.shu6(end);handles.shu6(end)=handles.shu6(end-1);handles.shu6(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu6);guidata(hObject, handles);function pushbutton18_Callback(hObject, eventdata, handles)handles.jieguo=get(handles.edit1, 'string' );handles.jieguo=strcat( '=' ,handles.jieguo);eval([ 'handles.result' '1' handles.jieguo]);set(handles.edit5, 'string'guidata(hObject,handles);pushbutton_0_Callback(hObject, eventdata, handles)handles.shu0=get(hObject, 'string' );handles.yuanshu=get(handles.edit1, 'string' ); if handles.yuanshu(end)== 'N' ;handles.yuanshu(end)= '' ;endif handles.yuanshu(1)== '0' ; handles.yuanshu=handles.yuanshu(2:end);end handles.shu0=strcat(handles.yuanshu,handles.shu0);if length(handles.shu0)<2elseif (length(handles.shu0)>=2)&&(handles.shu0(end-1)== (handles.shu0(1)== 'l' )temp=handles.shu0(end); handles.shu0(end)=handles.shu0(end-1);handles.shu0(end-1)=temp;endset(handles.edit1, 'string' ,handles.shu0); guidata(hObject, handles);function pushbutton20_Callback(hObject, eventdata, handles) handles.shu10=get(hObject, 'string' ); handles.yuanshu=get(handles.edit1, 'string' );handles.shu10=strcat(handles.yuanshu,handles.shu10); set(handles.edit1, 'string' ,handles.shu10); guidata(hObject, handles); ,num2str(handles.result1)); function ')' )&&function pushbutton21_Callback(hObject, eventdata, handles)function edit5_Callback(hObject, eventdata, handles)function edit5_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor' ),get(0, 'defaultUicontrolBackgroundColor' ))set(hObject, 'BackgroundColor' , 'white' ); endfunction pushbutton22_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' ); handles.fanhao=strcat( '-(' ,handles.yuanshu, ')' ); set(handles.edit1, 'string' ,handles.fanhao); guidata(hObject, handles);function pushbutton23_Callback(hObject, eventdata, handles)set(handles.edit1, 'string' , '0' );set(handles.edit5, 'string' , '0' );guidata(hObject, handles);function pushbutton24_Callback(hObject, eventdata, handles)result=questdlg('??卩?o a i ?3?£ ?; ' i ?3? e^e ?"「?•:' e ???',' e ???');if result== ' e・?", 'lose(gcf); endfunction pushbutton25_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.sin=strcat( 'sin(' ,handles.yuanshu, ')' );set(handles.edit1, 'string' ,handles.sin);guidata(hObject, handles);function pushbutton26_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.cos=strcat( 'cos(' ,handles.yuanshu, ')' );set(handles.edit1, 'string' ,handles.cos);guidata(hObject, handles);function pushbutton27_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.tan=strcat( 'tan(' ,handles.yuanshu, ')' );set(handles.edit1, 'string' ,handles.tan);guidata(hObject, handles);function pushbutton28_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.ln=strcat( 'reallog(' ,handles.yuanshu, ')' );set(handles.edit1, 'string' ,handles.ln);guidata(hObject, handles);function pushbutton29_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.log=strcat( 'log' ,handles.yuanshu, '()' );set(handles.edit1, 'string' ,handles.log);guidata(hObject, handles);function pushbutton30_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.daoshu=strcat( '1/(' ,handles.yuanshu, ')' ); set(handles.edit1, 'string' ,handles.daoshu);guidata(hObject, handles);function pushbutton31_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.sqrt=strcat( 'sqrt(' ,handles.yuanshu, ')' ); set(handles.edit1,'string' ,handles.sqrt);guidata(hObject, handles);function pushbutton32_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.pingfang=strcat( '(',ha ndles.yua nshu, ')A2'); set(handles.edit1, 'string' ,handles.pingfang);guidata(hObject, han dies);function pushbutton33_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.ncifang=strcat( '(' ,handles.yuanshu, ')AN' ); set(handles.edit1, 'string' ,handles.ncifang);guidata(hObject, handles);function pushbutton35_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.kuohao=strcat( '(' ,handles.yuanshu, ')' );set(handles.edit1, 'string' ,handles.kuohao);guidata(hObject, handles);function pushbutton36_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.yuanshu=handles.yuanshu(1:(end-1));if length(handles.yuanshu)<1handles.yuanshu= '0' ;endset(handles.edit1, 'string' ,handles.yuanshu);guidata(hObject, handles);% --- Executes on button press in pushbutton37.function pushbutton37_Callback(hObject, eventdata, handles)handles.yuanshu=get(handles.edit1, 'string' );handles.exp=strcat( 'exp(' ,handles.yuanshu, ')' );set(handles.edit1, 'string' ,handles.exp);guidata(hObject, handles);% --- Executes when user attempts to close figure1.function figure1_CloseRequestFcn(hObject, eventdata, handles)% hObject handle to figure1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Hint: delete(hObject) closes the figure delete(hObject);%function Untitled_14_Callback(hObject, eventdata, handles)% hObject handle to Untitled_14 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function Untitled_15_Callback(hObject, eventdata, handles)set(gcf, 'color' , 'red' )% hObject handle to Untitled_15 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function Untitled_16_Callback(hObject, eventdata, handles)set(gcf, 'color' , 'blue' )% hObject handle to Untitled_16 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function Untitled_17_Callback(hObject, eventdata, handles)set(gcf,% hObject 'color' , 'green' ) handle to Untitled_17 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)function Untitled_18_Callback(hObject, eventdata, handles)set(gcf,% hObject 'color' , 'black' ) handle to Untitled_18 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)function Untitled_19_Callback(hObject, eventdata, handles)set(gcf,% hObject 'color' , 'yellow' ) handle to Untitled_19 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)Untitled_20_Callback(hObject, eventdata, handles) functionset(gcf, 'color' , 'm' )% hObject handle to Untitled_20 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function Untitled_1_Callback(hObject, eventdata, handles)function Untitled_2_Callback(hObject, eventdata, handles)function Untitled_3_Callback(hObject, eventdata, handles)function Untitled_4_Callback(hObject, eventdata, handles)function Untitled_4_CreateFcn(hObject, eventdata, handles)function Untitled_5_Callback(hObject, eventdata, handles)function Untitled_6_Callback(hObject, eventdata, handles)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、Newdon迭代法求解非线性方程function [x k t]=NewdonToEquation(f,df,x0,eps)%牛顿迭代法解线性方程%[x k t]=NewdonToEquation(f,df,x0,eps)%x:近似解%k:迭代次数%t:运算时间%f:原函数,定义为内联函数�:函数的倒数,定义为内联函数%x0:初始值%eps:误差限%%应用举例:%f=inline('x^3+4*x^2-10');�=inline('3*x^2+8*x');%x=NewdonToEquation(f,df,1,0.5e-6)%[x k]=NewdonToEquation(f,df,1,0.5e-6)%[x k t]=NewdonToEquation(f,df,1,0.5e-6)%函数的最后一个参数也可以不写。
默认情况下,eps=0.5e-6 %[x k t]=NewdonToEquation(f,df,1)if nargin==3eps="0".5e-6;endtic;k=0;while 1x="x0-f"(x0)./df(x0);k="k"+1;if abs(x-x0) < eps || k >30break;endx0=x;endt=toc;if k >= 30disp('迭代次数太多。
');x="0";t="0";end2、Newdon迭代法求解非线性方程组function y="NewdonF"(x)%牛顿迭代法解非线性方程组的测试函数%定义是必须定义为列向量y(1,1)=x(1).^2-10*x(1)+x(2).^2+8;y(2,1)=x(1).*x(2).^2+x(1)-10*x(2)+8;return;function y="NewdonDF"(x)%牛顿迭代法解非线性方程组的测试函数的导数y(1,1)=2*x(1)-10;y(1,2)=2*x(2);y(2,1)=x(2).^+1;y(2,2)=2*x(1).*x(2)-10;return;以上两个函数仅供下面程序的测试function [x k t]=NewdonToEquations(f,df,x0,eps)%牛顿迭代法解非线性方程组%[x k t]=NewdonToEquations(f,df,x0,eps)%x:近似解%k:迭代次数%t:运算时间%f:方程组(事先定义)�:方程组的导数(事先定义)%x0:初始值%eps:误差限%%说明:由于虚参f和df的类型都是函数,使用前需要事先在当前目录下采用函数M文件定义% 另外在使用此函数求解非线性方程组时,需要在函数名前加符号“@”,如下所示%%应用举例:%x0=[0,0];eps=0.5e-6;%x=NewdonToEquations(@NewdonF,@NewdonDF,x0,eps)%[x k]=NewdonToEquations(@NewdonF,@NewdonDF,x0,eps)%[x k t]=NewdonToEquations(@NewdonF,@NewdonDF,x0,eps)%函数的最后一个参数也可以不写。
默认情况下,eps=0.5e-6%[x k t]=NewdonToEquations(@NewdonF,@NewdonDF,x0,eps)if nargin==3eps="0".5e-6;endtic;k=0;while 1x="x0-inv"(df(x0))*f(x0);%此处可采用其他方法避免求逆k="k"+1;if norm(x-x0) < eps || k > 15break;endx0=x;endt=toc;if k >= 15disp('迭代次数太多。
');x="zeros"(size(x0));t="0";end3、Lagrange插值法提供两个程序,采用了不同的方法function f="InterpLagrange"(x,y,x0)%构造Lagrange插值多项式%此函数中借助向量卷积来求Lagrange基函数,运算速度较快%f=InterpLagrange(x,y,x0)%f:插值多项式或者是插值多项式在x0处的值%x:节点%y:函数值%x0:某一测试点%%调用格式:%f=InterpLagrange(x,y) 返回插值多项式%f=InterpLagrange(x,y,x0) 返回插值多项式在点x0处的值%举例:%x=[0.32 0.34 0.36];y=[0.314567 0.333487 0.352274];x0=0.33;%f=InterpLagrange(x,y)%f=InterpLagrange(x,y,x0)if length(x)==length(y)n="length"(x);elsedisp('节点个数和函数值个数不同!')f=' ';return;endp=0;for i="1:n"l="y"(i);for j="1:n"if j==icontinue;end%利用卷积计算Lagrange基函数l=conv(l,[1 -x(j)]./(x(i)-x(j)));end%p是一向量,表示插值多项式的系数p="p"+l;endif nargin==3f="polyval"(p,x0);%计算插值多项式在x0处的值elsef="poly2str"(p,'x');%把插值多项式的向量形式转化为插值多项式的符号形式endfunction f="InterpLagrange2"(x,y,x0)%构造Lagrange插值多项式%此函数中借助符号运算来求Lagrange基函数,运算速度较慢,不推荐此种方法%f=InterpLagrange2(x,y,x0)%f:插值多项式或者是插值多项式在x0处的值%x:节点%y:函数值%x0:某一测试点%%调用格式:%f=InterpLagrange2(x,y) 返回插值多项式%f=InterpLagrange2(x,y,x0) 返回插值多项式在点x0处的值%举例:%x=[0.32 0.34 0.36];y=[0.314567 0.333487 0.352274];x0=0.33; %f=InterpLagrange2(x,y)%f=InterpLagrange2(x,y,x0)if length(x)==length(y)n="length"(x);elsedisp('节点个数和函数值个数不同!')f=' ';return;endsyms t;f=0;for i="1:n"l="y"(i);for j="1:n"if j==icontinue;endl=l*(t-x(j))/(x(i)-x(j));%借助符号运算,计算Lagrange基函数endf="f"+l;simplify(f);%化简多项式if i==nif nargin==3f="subs"(f,'t',x0);%计算插值多项式f在点x0处的值elsef="collect"(f);%计算插值多项式,展开并合并同类项f="vpa"(f,6);%设置多项式系数的有效数字endendend4、Newdon插值法function f="InterpNewdon"(x,y,x0)%Newdon插值多项式%f=InterpNewdon(x,y,x0)%f:插值多项式或者是插值多项式在x0处的值%x:节点%y:函数值%x0:某一测试点%%调用格式%f=InterpNewdon(x,y) 返回插值多项式%f=InterpNewdon(x,y,x0) 返回插值多项式在x0点的值%应用举例:%x=[1 2 3 4 5];y=[1 4 7 8 6];x0=6;%f=InterpNewdon(x,y)%f=InterpNewdon(x,y,x0)if length(x)==length(y)n="length"(x);elsedisp('节点个数和函数值个数不同!')f=' ';return;endA=zeros(n);%初始化差商矩阵for i="1:n"A(i,1)=y(i);%差商矩阵的第一列是函数值end%计算差商矩阵%差商矩阵中对角线上的元素为Newdon插值多项式的系数for j="2:n"for i="j:n"A(i,j)=(A(i,j-1)-A(i-1,j-1))/(x(i)-x(i-j+1));endend%求Newdon插值多项式p=zeros(1,n);for i="1:n"p1=A(i,i);%差商矩阵对角线上的元素就是Newdon插值多项式的系数 for j="1:i-1"p1=conv(p1,[1 -x(j)]);%计算Newdon插值多项式的基项endp1=[zeros(1,n-i),p1];%向量相加,维数必须相同。
把向量的元素补齐p="p"+p1;endif nargin==3f="polyval"(p,x0);%计算插值多项式在x0处的值elsef="poly2str"(p,'x');%把插值多项式的向量形式转化为插值多项式的符号形式end5、基本Guass消去法求解线性方程组function x="EqtsBasicGuass"(A,b)%基本Guass消去法求解线性方程组Ax=b%x=EqtsBasicGuass(A,b)%x:解向量,列向量%A:线性方程组的矩阵%b:列向量%%应用举例:%A=[2 2 3;4 7 7;-2 4 5]; b=[3;1;-7];%x=EqtsBasicGuass(A,b)%检查输入参数if size(A,1) ~= size(b,1)disp('输入参数有误!');x=' ';return;end%(A|b)A=[A b];%消去过程n=size(A,1);l=zeros(n);for k="1:n-1"for i="k"+1:nl(i,k)=A(i,k)/A(k,k);endfor i="k"+1:nfor j="k"+1:n+1A(i,j)=A(i,j)-l(i,k)*A(k,j);endfor j="1:k"A(i,j)=0;endendend%回代过程x=zeros(n,1);x(n)=A(n,n+1)/A(n,n);for i="n-1:-1:1"y="0";for j="i"+1:ny=y+A(i,j)*x(j);endx(i)=(A(i,n+1)-y)/A(i,i);endreturn;6、三角分解法求解线性方程组function x="EqtsDoolittleLU"(A,b)%Doolittle分解法求解线性方程组Ax=b%x=EqtsDoolittleLU(A,b)%x:解向量,列向量%A:线性方程组的矩阵%b:列向量%%应用举例:%A=[6 2 1 -1;2 4 1 0;1 1 4 -1;-1 0 -1 3];b=[6;-1;5;-5]; %x=EqtsDoolittleLU(A,b)%检查输入参数if size(A,1) ~= size(b,1)disp('输入参数有误!');x=' ';return;end%分解%把L和U的元素存储在A的相应位置上n=length(b);for k="1:n"for j="k:n"z=0;for r="1:k-1"z="z"+A(k,r)*A(r,j);endA(k,j)=A(k,j)-z;endfor i="k"+1:nz=0;for r="1:k-1"z="z"+A(i,r)*A(r,k);endA(i,k)=(A(i,k)-z)/A(k,k);endend%求解x=zeros(size(b));for i="1:n"z="0";for k="1:i-1"z=z+A(i,k)*x(k);endx(i)=b(i)-z;endfor i="n:-1:1"z="0";for k="i"+1:nz=z+A(i,k)*x(k);endx(i)=(x(i)-z)/A(i,i);endreturn7、追赶法求解三对角线性方程组function x="EqtsForwardAndBackward"(L,D,U,b)%追赶法求解三对角线性方程组Ax=b%x=EqtsForwardAndBackward(L,D,U,b)%x:三对角线性方程组的解%L:三对角矩阵的下对角线,行向量%D:三对角矩阵的对角线,行向量%U:三对角矩阵的上对角线,行向量%b:线性方程组Ax=b中的b,列向量%%应用举例:%L=[-1 -2 -3];D=[2 3 4 5];U=[-1 -2 -3];b=[6 1 -2 1]'; %x=EqtsForwardAndBackward(L,D,U,b)%检查参数的输入是否正确n=length(D);m=length(b);n1=length(L);n2=length(U);if n-n1 ~= 1 || n-n2 ~= 1 || n ~= mdisp('输入参数有误!')x=' ';return;end%追的过程for i="2:n"L(i-1)=L(i-1)/D(i-1);D(i)=D(i)-L(i-1)*U(i-1);endx=zeros(n,1);x(1)=b(1);for i="2:n"x(i)=b(i)-L(i-1)*x(i-1);end%赶的过程x(n)=x(n)/D(n);for i="n-1:-1:1"x(i)=(x(i)-U(i)*x(i+1))/D(i);endreturn;8、主元素的Guass消去法求解线性方程组function x="EqtsClmnPrimElemGuass"(A,b)%主元素的Guass消去法求解线性方程组Ax=b%x=EqtsClmnPrimElemGuass(A,b)%x:解向量,列向量%A:线性方程组的矩阵%b:列向量%%应用举例:%A=[-0.002 2 2;1 0.78125 0;3.996 5.5625 4];%b=[0.4;1.3816;7.4178];%x=EqtsClmnPrimElemGuass(A,b)%检查输入参数if size(A,1) ~= size(b,1)disp('输入参数有误!');x=' ';return;end%(A|b)A=[A b];%消去过程n=size(A,1);l=zeros(n);for k="1:n-1"%换行[a idx1]=max(abs(A(k:n,k)));%寻找绝对值最大的元素的下标[b idx2]=min(abs(A(k:n,k)));%寻找绝对值最小的元素的下标 idx1=idx1+k-1;idx2=idx2+k-1;for j="1:n"+1c=A(idx1,j);A(idx1,j)=A(idx2,j);A(idx2,j)=c;endfor i="k"+1:nl(i,k)=A(i,k)/A(k,k);endfor i="k"+1:nfor j="k"+1:n+1A(i,j)=A(i,j)-l(i,k)*A(k,j);endfor j="1:k"A(i,j)=0;endendend%回代过程x=zeros(n,1);x(n)=A(n,n+1)/A(n,n);for i="n-1:-1:1"y="0";for j="i"+1:ny=y+A(i,j)*x(j);endx(i)=(A(i,n+1)-y)/A(i,i);endreturn;9、Euler法求解常微分方程function outXY="ODEEuler"(f,x0,y0,h,PointNum)%简单欧拉法求解常微分方程dy/dx=f%outXY=ODEEuler(f,x0,y0,h,PointNum)%outXY:所取点的横纵坐标。