您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > 计算方法常用计算编程
1.向量范数:11niixx12221niixx1maxiinxx11nppipixxclearx=input('输入一个向量\nx=')x1=sum(abs(x))%1-范数x2=sqrt(sum(abs(x).^2))%2-范数xinf=max(abs(x))%无穷-范数p=input('多少范数?输入p=')xp=(sum(abs(x).^p)).^(1/p)%p-范数2.矩阵范数:111maxnijjniAamax2TAAA11maxnijinjAa211nnijFijAacleara=input('输入一个矩阵\na=')x1=max(sum(abs(a)))%1-范数x2=sqrt(max(eig(a'*a)))%2-范数xinf=max(sum(abs(a)'))%无穷-范数xF=sqrt(sum(sum(a.^2)))%F-范数3.高斯消去法:设Ax=b,ijnAa,则11(,1,...,)kikikkkkkkkijijikkjkkkiiikkaLaaaLaijknbbLb1(1,2,...,2,1)nnnnnnnkkkkjjjkkkkkbxaknnbaxxacleara=input('输入一个系数矩阵\na=')b=input('输入一个自由项\nb=')n=length(b);fork=1:n-1fori=k+1:nl(i,k)=-a(i,k)/a(k,k);forj=k:na(i,j)=a(i,j)+l(i,k)*a(k,j);endb(i)=b(i)+l(i,k)*b(k);endendzengguang=[a,b]x(n)=b(n)/a(n,n);fork=n-1:-1:1t=0;forj=k+1:nt=t+a(k,j)*x(j)endx(k)=(b(k)-t)/a(k,k);endx输入一个系数矩阵a=[111;04-1;2-21]a=11104-12-21输入一个自由项b=[6;5;1]b=651zengguang=111604-1500-2-6x=1234.判断一个数字是否是素数。cleara=input('a=')fori=2:a-1ifrem(a,i)==0sprintf('%d不是素数',a)breakendendifa==i+1sprintf('%d是素数',a)endclearclca=input('输入系数矩阵a=')b=input('输入自由项b=')n=length(b);D=diag(diag(a))L=zeros(n);U=zeros(n);fori=1:n-1L(i+1:n,i)=-a(i+1:n,i)endfori=1:n-1U(i,i+1:n)=-a(i,i+1:n)endx1=input('迭代初始值x1=')x1=x1';derta=input('输入精度要求derta=')ifderta=0derta=0.00001end%%%%%%%%%%%%%%%%雅克比迭代法矩阵迭代%t=1;while(tderta)x2=inv(D)*(L+U)*x1+inv(D)*b;t=max(abs(x1-x2));x1=x2;endinput('回车后输出雅克比迭代法矩阵迭代的解x=')x2%%%%%%%%%%%%%%雅克比迭代法元素迭代法%t=1;while(tderta)fori=1:ntt=0;forj=1:i-1tt=tt+a(i,j)*x1(j);endttt=0;forj=i+1:nttt=ttt+a(i,j)*x1(j);endx2(i)=(b(i)-tt-ttt)/a(i,i);endt=max(abs(x1-x2));x1=x2;endinput('回车后输出雅克比迭代法元素迭代的解x=')x2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%高斯迭代法矩阵迭代%t=1;while(tderta)x2=inv(D-L)*U*x1+inv(D-L)*b;t=max(abs(x1-x2));x1=x2;endinput('回车后输出高斯迭代法矩阵迭代的解x=')x2%%%%%%%%%%%%%%高斯迭代法元素迭代法%t=1;while(tderta)fori=1:ntt=0;forj=1:i-1tt=tt+a(i,j)*x2(j);endttt=0;forj=i+1:nttt=ttt+a(i,j)*x1(j);endx2(i)=(b(i)-tt-ttt)/a(i,i);endt=max(abs(x1-x2));x1=x2;endinput('回车后输出高斯迭代法元素迭代的解x=')x2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%逐次超松弛迭代法矩阵迭代%t=1;w=input('输入超松弛因子w=')ifw0&w=2w=w;elsew=0.15;endwhile(tderta)x2=inv(D-w*L)*((1-w)*D+w*U)*x1+w*inv(D-w*L)*b;t=max(abs(x1-x2));x1=x2;endinput('回车后输出超松弛迭代法矩阵迭代的解x=')x2%%%%%%%%%%%%%%超松弛迭代法元素迭代法%t=1;while(tderta)fori=1:ntt=0;forj=1:i-1tt=tt+a(i,j)*x2(j);endttt=0;forj=i+1:nttt=ttt+a(i,j)*x1(j);endx2(i)=w*(b(i)-tt-ttt)/a(i,i)+x1(i);endt=max(abs(x1-x2));x1=x2;endinput('回车后输出超松弛迭代法元素迭代的解x=')x2%输入系数矩阵a=[-4111;1-411;11-41;111-4]%输入自由项b=[1;1;1;1]%迭代初始值x1=[0000]%输入精度要求derta=0.000046cleara=input('任意输入一个数a=')fork=2:a-1ifrem(a,k)==0sprintf('%d不是素数',a)breakendendifk==a-1sprintf('%d是素数',a)end%%%向量范数通用程序clearx=input('输入一个向量\nx=')x1=sum(abs(x))%1-范数x2=sqrt(sum(abs(x).^2))%2-范数xinf=max(abs(x))%无穷-范数p=input('多少范数?输入p=')xp=(sum(abs(x).^p)).^(1/p)%p-范数%%%矩阵范数通用程序cleara=input('输入一个矩阵\na=')x1=max(sum(abs(a)))%1-范数x2=sqrt(max(eig(a'*a)))%2-范数xinf=max(sum(abs(a)'))%无穷-范数xF=sqrt(sum(sum(a.^2)))%F-范数%%%高斯顺序消去法通用程序cleara=input('输入一个系数矩阵\na=')b=input('输入一个自由项\nb=')n=length(b);fork=1:n-1fori=k+1:nl(i,k)=-a(i,k)/a(k,k);forj=k:na(i,j)=a(i,j)+l(i,k)*a(k,j);endb(i)=b(i)+l(i,k)*b(k);endendzengguang=[a,b]x(n)=b(n)/a(n,n);fork=n-1:-1:1t=0;forj=k+1:nt=t+a(k,j)*x(j)endx(k)=(b(k)-t)/a(k,k);endxclearclcx0=input('输入初值x0=')ifabs(x0)0x0=x0;elsex0=1.5endx=sym('x');%f='x-x^3-4*x^2+10'f=input('输入迭代函数f=')t=1;pp=1;%%%%%%下面判断迭代是否收敛g=diff(f);x=x0;gg=eval(g)ifabs(gg)1x1='迭代格式不收敛'elsederta=input('输入迭代精度derta=')ifabs(derta)0derta=derta;elsederta=0.000001;endt=1;x=x0;while(tderta)pp=pp+1;x1=eval(f);t=abs(x1-x);x=x1;ifpp10000%如果不收敛,则迭代10000次后自动终止x1='迭代超过10000次';break;endendendpx1%f='x-x^3-4*x^2+10'%f='1/2*(10-x^3)^(1/2)'%f='(10/x-4*x)^(1/2)'%f='(10/(4+x))^(1/2)'%f='x-(x^3+4*x^2-10)/(3*x^2+8*x)'%%%%%%%%%%%%%%%%结果展示%输入初值x0=1.5%x0=%1.5000%输入迭代函数f='x-x^3-4*x^2+10'%f=%x-x^3-4*x^2+10%输入迭代精度derta=0.00002%derta=%2.0000e-005%x1=%NaNclearclcx0=input('输入初值x0=')ifabs(x0)0x0=x0;elsex0=1.5endx=sym('x');%f='x-x^3-4*x^2+10'f=input('输入迭代函数f=')t=1;pp=1;%%%%%%下面判断迭代是否收敛g=diff(f);x=x0;gg=eval(g);ifabs(gg)1x1='迭代格式不收敛'elsederta=input('输入迭代精度derta=')ifabs(derta)0derta=derta;elsederta=0.000001;endt=1;x=x0;w=1/(1-gg);%加权因子while(tderta)pp=pp+1;x1=w*x+(1-w)*eval(f);%两个组合的叠加t=abs(x1-x);x=x1;ifpp10000%如果不收敛,则迭代10000次后自动终止x1='迭代超过10000次';break;endendendpx1%f='x-x^3-4*x^2+10'%f='1/2*(10-x^3)^(1/2)'%f='(10/x-4*x)^(1/2)'%f='(10/(4+x))^(1/2)'%f='x-(x^3+4*x^2-10)/(3*x^2+8*x)'%%%%%%%%%%%%%%%%clearclcx0=input('输入初值x0=')ifabs(x0)0x0=x0;elsex0=1.5endx=sym('x');%f='x-x^3-4*x^2+10'f=input('输入迭代函数f=')t=1;pp=1;%%%%%%下面判断迭代是否收敛g=diff(f);x=x0;gg=eval(g);ifabs(gg)1x1='迭代格式不收敛'elsederta=input('输入迭代精度derta=')ifabs(derta)0derta=derta;elsederta=0.000001;endt=1;x=x0;w=1/(1-gg);%加权因子while(tderta)pp=pp+1;x2=x;y=eval(f);x=y;z=eval(f);x=x2;x1=x-(y-x)^2/(z-2*y+x);%两个组合的叠加t=abs(x1-x);x=x1;ifpp10000%如果不收敛,则迭代10000次后自动终止x1='迭代超过10000
本文标题:计算方法常用计算编程
链接地址:https://www.777doc.com/doc-4028764 .html