lzmagic  
1/4桶水荡漾
日历
<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
统计
  • 随笔 - 7
  • 文章 - 0
  • 评论 - 0
  • 引用 - 0

导航

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

2008年5月27日

3.1符号表达式的生成
       1.在数值运算中,在输入、输出、中间过程中,变量都是数值变量;
          在符号运算中,在输入、输出、中间过程中,变量都是以字符串形式保存和运算的(数字也是)。
       2.符号表达式: 符号函数(无等号) + 符号方程(有等号)
       3.符号函数的生成: >>f = 'log(x)';
          符号方程的生成: >>e = 'a*x^2+b*x+c=0';
                                        >>d = 'Dy - y = x'; %符号微分方程
                                       ·用这种方法创建符号表达式对空格很敏感,不要在符号之间乱加空格符。
       4.sym命令: 符号函数的生成: >>f = sym('log(x)');
                         符号方程的生成: >>e = sym('a*x^2+b*x+c=0');
       5.syms命令: 只能创建符号函数: >>syms x  >>f=log(x)
3.2符号和数值之间的转换
       1.digits函数:  digits(D)  %函数设置有效数位为D的近似解,仅对
       2.vpa函数: vpa(S)   %符号表达式S在digits函数设置下的近似解
                         vpa(S,D)  %符号表达式S在digits(D)设置下的近似解
       3.subs函数: subs(S,OLD,NEW)  %用NEW变量代替符号表达式S中的OLD变量
       4.numeric函数: numeric(S)  %将符号表达式S转换为数值形式
          double函数: double(S)  %将符号表达式S转换为数值形式(同numeric)
3.3符号函数的运算
       3.3.1复合函数: compose函数
                                 compose(f,g)   %f = f(x), g = g(y)
                                 compose(f,g,z)   %f = f(x), g = g(y), 用g代替x,用z代替y
                                 compose(f,g,x,z)  %f = f(x), g = g(y), 用g代替x,用z代替y
                                 compose(f,g,x,y,z)  %f = f(x), g = g(y), 用g代替x,用z代替y
       3.3.2反函数: finverse函数
                             finverse(f)   %默认以变量x给出结果
                             finverse(f,v)   %以v为自变量,最后f要转换为v
3.4符号矩阵的创建
       3.4.1用sym函数创建
       3.4.2用子阵创建: (不提倡)
       3.4.3将数值矩阵转化为符号矩阵: b=sym(a) %a为数值矩阵,无论a的元素是用分数还是浮点数表示,转化后的符号矩阵都是用最接近的精确有理数形式表示。
       3.4.4符号矩阵的索引和修改: 索引跟数值矩阵的一样  %>>b(2,3)
                                                         修改要用一个字符串代替 %>>b(2,3)='log(9)';
3.5符号矩阵的运算
       3.5.1基本运算
              1.四则运算
                     1)加减: + -
                     2)乘除: * / \
                     3)转置: '
              2.行列式运算:  det
              3.逆:   inv
              4.秩:  rank
              5.幂运算: ^
              6.指数运算: exp
                                   expm
       3.5.2矩阵分解
              1.特征值分解函数eig
              2.奇异值分解函数svd
              3.约当标准型函数jordan
              4.三角抽取函数diag, tril, triu
       3.5.3矩阵的空间运算
              1.列空间运算函数colspace
              2.零空间运算函数null
       3.5.4符号矩阵的简化
              1.因式分解: factor(S) %为了分解大于2^52的整数,可使用factor(sym('N'))
              2.展开: expand(S)
              3.合并同类项: collect(S)
                                       collect(S,v)
              4.符号简化: simple(S)  %对表达式S尝试多种不同的算法,以显示长度最短的简化形式,若S为一符号矩阵,结果是全矩阵的最简型式,而不是每个元素的最简型式。
                                  simplify(S)  %简化符号矩阵的每一个元素。
              5.分式通分: [N,D]=numden(A) %Numerator and denominator of a symbolic expression.
              6.秦九昭型: horner(P)
3.6符号微积分
       3.6.1符号极限: limit函数
                                 limit(F)  %计算a=0时的极限
                                 limit(F,a) %计算finsym(F)->a时的极限
                                 limit(F,x,a) %计算x->a时的极限
                                 limit(F,x,a,'left'(或者'right')) %用left或right指定求极限的方向
       3.6.2符号积分: int函数
                                 int(S)  %计算findsym(S)的不定积分(S为常数时默认变量为x)
                                 int(S,v)  %v为变量
                                 int(S,a,b) %计算findsym(S)的从a到b的定积分
                                 int(S,v,a,b) %v为变量
       3.6.3符号微分
              1.微分函数diff: diff(S)
                                 diff(S,'v')或diff(S,sym('v'))
                                 diff(S,n) %求n次微分
                                 diff(S,'v',n)或diff(S,n,'v')
              2.梯度函数gradient: [FX,FY,FZ,...]=gradient(F) %默认各方向点间隔为1
                                               [FX,FY,FZ,...]=gradient(F,H)%默认各方向点间隔为H
                                               [FX,FY,FZ,...]=gradient(F,HX,HY,HZ)
              3.多元函数导数: jacobian(f,v) %计算f对v的Jocobi矩阵
                                                               %>>syms x y z
                                                               %>>jacobian([x^2+y^2;x^2-y^2],[x,y])
                                                               %>>ans = [ 2*x, 2*y]
                                                               %        [ 2*x,-2*y]
3.7符号代数方程求解
       3.7.1线性方程组的符号解法
              1.linsolve(A,B) %X = LINSOLVE(A,B) is the same as X = sym(A)\sym(B).
              2.solve函数
                solve('eqn1','eqn2',...,'eqnN')
                solve('eqn1','eqn2',...,'eqnN','var1,var2,...,varN')
                solve('eqn1','eqn2',...,'eqnN','var1','var2',...'varN')
       3.7.2非线性方程组的符号解法: fsolve函数
                                                             fsolve('fun',X0)
3.8符号微分方程求解
       常微分方程的符号解: dsolve('eqn1','eqn2',...,'eqnN')
3.9符号函数的二维图
       3.9.1ezplot
               ezplot(f)  %f秩包括单个符号变量x的符号表达式,默认[-2*pi,2*pi]
               ezplot(f,xmin,xmax)或ezplot(f,[xmin,xmax])
               ezplot(f,[xmin,xmax],fig) %指定绘图的图窗口代替当前窗口
 3.9.2fplot
         fplot(fun,lims)  %fun为M文件或对x的符号表达式,lims为[xmin,xmax]或[xmin,xmax,ymin,ymax]
         fplot(fun,lims,tol) %tol < 1,相对误差,默认tol = 0.002
         fplot(fun,lims,n) %n >= 1, 以最少n + 1个点来绘图
         fplot(fun,lims,'LineSpec') %指定线型绘图
3.10图示化函数计算器
         funtool函数
3.11Maple接口
posted @ 2008-05-27 13:48 lzmagic 阅读(74) | 评论 (0)编辑 收藏

2008年5月21日

2.1Matlab的数据类型
       2.1.1变量与常量
              1.变量: 1)优点: 无需事先声明和指定变量类型
                           2)命名规则: 以字母开头,区分大小写,长度不超过31个字符
                           3)作用域: 默认为局部变量;全局变量前加global
              2.常量: i,j 虚数单位 Realmin  最小正浮点数,2^-1022
                           pi 圆周率  Realmax  最小正浮点数,2^1023
                           eps 相对精度10^-52 Inf  无穷大
                           NaN Not-a-Number
                           ·可以修改这些预定义的常量值,通过“clear+常量名”即可恢复初始值。
       2.1.2数字变量 
              1.若未对表达式设定赋值变量,Matlab会自动赋当前结果给ans变量。
              2.输入格式同C语言,输出格式: format
       2.1.3字符串
              1.字符串: 用单引号相括,行向量->s='abc'
                 字符数组: 用函数char,列向量->s=char('a','b','c')
                ·在Matlab中,字符串和字符数组(或矩阵)基本上是等价的。
              2.字符串与数值数组的转换:
                 num2str  数字->字符串           str2num   字符串->数字 %double
                 int2str  整数->字符串              mat2str   矩阵->字符串
                 sprintf  格式数据->字符串      sscanf   读字符串
              3.字符串操作: 1)strcat
                                       2)strcmp
                                       3)finstr(S1,S2) %在S1串中查找S2串,输出开头坐标向量
                                       4)upper
                                       5)lower
              4.字符串执行: eval(字符串)
       2.1.4矩阵
       2.1.5单元型变量: 一种以任何形式的数组为元素的多维数组。
              1.变量定义: 1)用大括号"{}"直接输入
                                   2)用cell函数
              2.用{}作下标完全显示该元素,用()作下标显示该元素的压缩形式。
              3.可以嵌套,单元型变量可以是单元型变量。
       2.1.6结构型变量: 一种将不用数据类型组合起来的数据类型。
              1.变量定义: 1)用点符号"."直接输入
                                   2)用struct函数: 
                                           结构型变量名=struct('属性名1',属性值1,'属性名2',属性值2,...)
              2.用指针方式访问属性,当属性多于一个时键入变量名不能完全显示,只显示各属性名。
              3.可以嵌套,结构型变量可以是结构型变量。
2.2向量及其运算
       2.2.1向量的生成
              1.直接输入向量,使用中括号"[]"
              2.用冒号表达式: x=x0:step:xn
              3.线性等分向量的生成:  y=linspace(x0,x99)          %默认n=100
                                                        y=linspace(x0,xn-1,n)
                 对数等分向量的生成:  y=logspace(x0,x49)         %默认n=50
                                                        y=logspace(x0,xn-1,n)
       2.2.2向量的运算
              1.点乘: dot(a,b)                 %默认dim=1
                           dot(a,b,dim)
              2.叉乘: cross(a,b)              %a和b必须是三维向量(大于3就取前三维)
                           cross(a,b,dim)       %a和b必须同维且size(a,dim)=size(b,dim)=3
2.3矩阵及其运算
       2.3.1矩阵的生成
              1.用"[]"直接输入
              2.用M文件输入  %通常输入大矩阵
       2.3.2矩阵的基本数学运算
              1.矩阵的加减法
              2.矩阵的乘法
               ·A的列数和B的行数相同时,A和B才可以相乘
              3.矩阵的除法
                     1)左除"\" %效果好些
                     2)右除"/"
              4.矩阵与常数的运算
               ·数除时,常数通常只能做除数。
              5.矩阵的逆运算: inv函数
              6.矩阵的行列式运算: det函数
              7.矩阵的幂运算: ^
              8.矩阵的指数运算: expm函数
                     1)expm1: Pade近似
                     2)expm2: Tayor级数
                     3)expm3: 特征值法
              9.矩阵的对数运算: logm函数
              10.矩阵的开方运算: sqrtm函数
       2.3.3矩阵的基本函数运算
              1.特征值函数
                     1)[V,D]=eig(A)
                     2)[V,D]=eigs(A)             %迭代法
              2.奇异值函数
                     1)[U,S,V]=svd(A)
                     2)[U,S,V]=svds(A)
              3.条件数函数
                     1)cond                           %矩阵的条件数的值
                     2)condest                       %矩阵的1范数条件数的值
                     3)rcond                          %矩阵的条件数的倒数值
              4.特征值的条件数
                                   condeig(A)
                     [V,D,s]=condeig(A)       %等价于[V,D]=eig(A), s=condeig(A)
              5.范数函数
                     1)norm
                     2)normest  %矩阵2范数的估计值
              6.秩函数: rank
              7.迹函数: trace
              8.零空间函数
              9.正交空间函数: orth
              10.伪逆函数: pinv
              11.通用函数形式: funm(A,'funname') %funm(a,'log')==logm(a) funm(a,'sqrt') == sqrtm(a)
       2.3.4矩阵的分解函数
              1.特征值分解
              2.复数特征值对角阵与实数块特征值对角阵的转化
              3.奇异值分解
              4.LU分解: lu
       2.3.5特殊矩阵
              1.空阵: (可以缩维)
              2.全0阵: zeros
              3.单位阵: eye
              4.全1阵: ones
              5.随机阵: rand
                             randn                                      %正态随机阵
       2.3.6矩阵的特殊操作
              1.变维: 1)":"符号表达式                        %只能在2矩阵间实现变维,而须预先定义两者的维数
                                                                            %a=[1:12];c=zeros(3,4);c(:)=a(:);
                          2)reshape(X,M,N,P,...)
              2.转向: 1)旋转: rot90(A)                        %逆时针旋转90度
                                        rot90(A,K)                   %逆时针旋转(90*K)度
                           2)翻转: fliplr(A)                         %左右翻转
                                        flipud(A)                       %上下翻转
                                        flipdim(A,dim)               %第dim维翻转
              3.抽取: 1)对角元素抽取: diag(X)          %diag(X,0)
                                                       diag(X,k)       %k为正为上方第k条对角线,k为0为主对角线,k为负为下方第k条对角线
                          2)上三角矩阵抽取: triu(X)        %triu(X,0)
                                                          triu(X,k)      %X的第k条对角线的上部分
                          3)下三角矩阵抽取: tril(X) %tril(X,0)
                                                           tril(X,k) %X的第k条对角线的下部分
              4.扩展: X(m1:m2,n1:n2)=A  %sizeof(A)=(m2-m1+1)*(n2-n1+1),其它元素为0
2.4数组及其运算
       2.4.1数组的基本运算
              1.数组和常数的加减预算可加可不加"."号,加"."号一定要把常数写在前面。
              2.矩阵与常数间的除法,常数只能做除数,数组没有这个限制。
              3.数组的指数运算、对数运算和开方运算相对矩阵来说都有了简化,分别为exp、log和sqrt
       2.4.2数组的函数运算: funm(A,'funname') %funname为常用的函数名,如sin
       2.4.3数组的逻辑运算
              1.基本运算: 优先级关系先后顺序:比较运算->算术运算->逻辑与或非运算
              2.函数运算: 1)all                          %全部为0为真
                                   2)any                        %存在不为0为真
                                   3)find                        %寻找非0坐标
                                      find(逻辑表达式)  %寻找逻辑表达式为真的坐标
2.5多项式运算
       2.5.1多项式的表达方法
              1.行向量: P=[a0,a1,...,an](')          %P(x)=a0x^n+a1x^n-1+...+an(降幂排列)
                               poly2sym(P)                  %ans=a0x^n+a1x^n-1+...+an
              2.特征多项式输入法: 求矩阵的特征多项式poly(A)    %首项系数一定为1
                                                                                                      %n阶特征矩阵一定产生n次多项式
              3.由根创建多项式: poly(向量)
       2.5.2多项式运算
              1.求值: 1)数组为单位: polyval(P,X)
                           2)矩阵为单位: polyvalm(P,X)    %Y=P(1)*X^N+P(2)*X^(N-1)+...+ P(N)*X+P(N+1)*I
              2.求根: 1)roots
                           2)先求伴随矩阵,再求其特征值:A=compan(P); R=eig(A)
              3.乘除法: 1)乘法: conv(P,D)                   %P,D不允许poly2sym后        %向量的卷积
                               2)除法: deconv(PD,D)            %PD,D不允许poly2sym后      %向量的解卷
              4.微分: polyder(P)  %P不允许poly2sym后,要sym2poly才行
              5.拟合: 1)由矩阵的除法求解超定方程
                           2)polyfit(X,Y,n) %X,Y为拟合数据,n为拟合多项式的阶数

posted @ 2008-05-21 00:30 lzmagic 阅读(158) | 评论 (0)编辑 收藏

2008年5月20日

1.1Matlab 6.X简介
       1.1.121世纪的科学计算语言
             1.功能强大
             2.语言简单
             3.扩充能力强、可开发性强
             4.编程易、效率高
       1.1.2Matlab的发展历史
       1.1.3Matlab的新特点
       1.1.4Matlab的应用和网上资源
1.2Matlab 6.X的安装
1.3Matlab的桌面平台
1.4帮助系统
      1.4.1联机帮助系统
             >>helpwin == helpdesk == doc     %Help|Full Product Family Help
      1.4.2命令窗口查询帮助系统
             >>help                                          %On-line help, display text at command line.
             >>lookfor                                     %Search all M-files for keyword.
             >>exist                                         %Check if variables or functions are defined.
             >>what                                        %List MATLAB-specific files in directory.
             >>who                                         %List current variables.
             >>whos                                        %List current variables, long form.
             >>which                                       %Locate functions and files.
      1.4.3联机演示系统
             >>demos          %Help|Demos
      1.4.4常用的命令和操作技巧
             1.常用命令
             >>cd                %Change current working directory.改变工作目录
             >>clc                %Clear command window.清除命令窗
             >>clear             %Clear variables and functions from memory.清除内存变量
             >>clf                 %Clear current figure.清除图形窗口
             >>diary             %Save text of MATLAB session.日志文件命令
             >>dir                %List directory.显示目录文件
             >>disp              %Display array.显示变量或文字内容
             >>echo             %Echo commands in M-files.显示命令窗信息
             >>hold on/off    %Hold current graph.图形保持开关
             >>load              %Load workspace variables from disk.加载指定mat文件的变量
             >>pack             %Consolidate workspace memory.收集内存碎片,扩大内存空间
             >>path              %Get/set search path.显示或设置搜索目录
             >>quit               %Quit MATLAB session.退出Matlab
             >>save              %Save workspace variables to disk.保存内存变量到指定mat文件
             >>type              %List M-file.显示mat文件内容
             >>!                   %调用DOS命令
       2.常用操作技巧
             >>up             >>down
             >>left            >>right
             >>Ctrl+left    >>Ctrl+right
             >>home        >>end
             >>backspace>>del
             >>esc
1.5Matlab搜索目录
             >>path                           %path('E:\myfile',path); path(path,'E:\myfile');
             >>editpath == pathtool   %File|Set Path
             >>addpath                     %Add directory to search path.加载目录

 

 

posted @ 2008-05-20 00:51 lzmagic 阅读(54) | 评论 (0)编辑 收藏

2008年5月10日

     摘要:   1/**//* (MST: minimum spanning tree)  2 * 描述: 设G = (V, E)是无向图,求最小生成树T(U, E')。  3 *  4 * ...  阅读全文
posted @ 2008-05-10 09:38 lzmagic 阅读(1011) | 评论 (0)编辑 收藏

2008年4月27日

Description

栈是常用的一种数据结构,有n个元素在栈顶端一侧等待进栈,栈顶端另一侧是出栈序列。你已经知道栈的操作有两种:push和pop,前者是将一个元素进栈,后者是将栈顶元素弹出。现在要使用这两种操作,由一个操作序列可以得到一系列的输出序列。请你编程求出对于给定的n,计算并输出由操作数序列1,2,…,n,经过一系列操作可能得到的输出序列总数。

Input

就一个数n(0<n<20)。

Output

一个数,即可能输出序列的总数目。

Sample Input

3

Sample Output

5
Solution 1st:
a[i][j] renotes the possible number of the previous j sequences with n = i
(we set a[k][0] = 1 (0 < k <= n), which implies NULL is thought of one possible sequence)
we init that a[1][1] = a[1][0] = 1;
yeild:
a[i][0] = a[i - 1][0];
a[i][j] = a[i][j - 1] + a[i - 1][j]; (1 <= j <= i - 1)
a[i][i] = a[i][i - 1];
the answer wo want to get is a[n][n].

 1#include<iostream>
 2using namespace std;
 3
 4#define MAXN 20
 5
 6int main()
 7{
 8    // init
 9    int a[MAXN][MAXN];
10    a[1][1= a[1][0= 1;
11
12    // input the total number pushed into the stack(0 < n && n < 20)
13    int n, i, j;
14    cin >> n;
15
16    // run
17    for(i = 2; i <= n; i++)
18    {
19        a[i][0= a[i - 1][0];
20        for(j = 1; j <= i - 1; j++)
21        {
22            a[i][j] = a[i][j - 1+ a[i - 1][j];
23        }

24        a[i][j] = a[i][j - 1];
25    }

26
27    // ouput the number of possible sequences poped from the stack
28    cout << a[n][n] << endl;
29    
30    return 0;
31}

Solution 2nd:
denote a[n] the number of possible sequences poped from the stack,
denote i the number of the set si poped before the first number pushed '1' poped, 
and denote i the number of the set sj poped after the first number pushed '1' poped,
then we can easily get the conlusion that no number in seti is bigger than any number in setj,
we can depart the sequence into two part and caculate them respectively,
hence the possible number is a[i] * a[j] (0 <= i <= n - 1), i.e.
      a[n] = a[0]*a[n - 1] + a[1]*a[n - 2] + …… + a[n - 2]*a[1] + a[n - 1]*a[0]

 1#include<iostream>
 2using namespace std;
 3
 4#define MAXN 20
 5
 6int main()
 7{
 8    int a[MAXN];
 9    a[0] = 1;
10    int n, i, j;
11    cin >> n;
12    for(i = 1; i <= n; i++)
13    {
14        for(a[i] = 0, j = 0; j < i; j++)
15            a[i] += a[j] * a[i - 1 - j];
16    }
17    for(i = 1; i <= n; i++)
18        cout << "a[" << i << "] = " << a[i] << endl;
19    return 0;
20}

Solution 3id:

This is Catalan Number :
a[n] = a[0]*a[n - 1] + a[1]*a[n - 2] + …… + a[n - 2]*a[1] + a[n - 1]*a[0]
a[n] = C(n , 2 * n) / (n + 1)
posted @ 2008-04-27 14:56 lzmagic 阅读(37) | 评论 (0)编辑 收藏

2008年4月12日

/*
 * 正整数划分问题:
 * 将正整数n表示成一系列正整数之和,n = n1 + n2 +……+ nk (n1 >= n2 >=……>= nk >= 1, k >= 1)
 * 正整数n的这种表示称为正整数n的划分。
 * 求正整数n的不同划分数pn。
 * Input: n  (End with 0)
 * Ouput: pn (in each line)
 */

 1// Sulotion 1st:
 2#include<iostream>
 3using namespace std;
 4
 5int Count(int n, int m)
 6{
 7     if(m == 1return 1;
 8    if(n <  m) return Count(n, n);
 9    if(n == m) return 1 + Count(n, m - 1);
10    if(n >  m) return Count(n - m, m) + Count(n, m - 1);
11}

12
13int main()
14{
15    int n;
16    cin >> n;
17    while(n != 0)
18    {
19        cout << Count(n, n) << endl;
20        cin >> n;
21    }

22
23    return 0;
24}

 1// Solution 2nd:
 2#include<iostream>
 3using namespace std;
 4
 5#define MAXN 100
 6
 7int main()
 8{
 9    int n, a[MAXN][MAXN], max, i, j;
10
11    // 初始值
12    a[1][1= 1;
13    max = 1;
14
15    cin >> n;
16    while(n != 0)
17    {
18        if(n > max)
19        {
20            for(i = max + 1; i <= n; i++)
21            {
22                a[i][1= a[i - 1][1];
23                for(j = 2; j <= i / 2; j++)
24                    a[i][j] = a[i - j][j] + a[i][j - 1];
25                for(; j <= i - 1; j++)
26                    a[i][j] = a[i - j][i - j] + a[i][j - 1];
27                a[i][j] = 1 + a[i][j - 1];
28            }

29            max = n;
30        }

31        cout << a[n][n] << endl;
32        cin >> n;
33    }

34    return 0;
35}
posted @ 2008-04-12 11:01 lzmagic 阅读(59) | 评论 (0)编辑 收藏

2008年3月30日

    Nice and Sunsan play the game of multiplication by ultiplying an integer p by one of the number2 2 to 9.Nic always starts with p = 1, does his multiplication.Then Susan multiplies the number, then Nic and so on. Before a game starts, they draw an integer 1<= n <= 4,294,967,295 and the winner is who first reaches p>=n.

Inuput: Each line of input contains one integer number n and ends while n = 0.
Output: For each line of input output one line either
            Nic wins.       
            or
            Susan wins.
            Assume that both of them play perfectly.
Sample Input:
162
17
34012226
0
Output for the Sample Input:
Nic wins.
Susan wins.
Nic wins.

 1/*Sulution: Nic(1st) wins over [1, 9], Susan(2nd) wins over [10, 18], Nic(1st) wins over [19, 162], Susan(2nd) wins over [163, 344], think a little and easy know why, then wo can get that 9 * 2 -> 18 * 9 -> 162 ->344……
 2*/

 3
 4#include<iostream>
</