MatLab之父:编程实践学习笔记(一)--迭代

1.    >> x=3 赋值

2.     单条语句用逗号或分号分隔

3.    >> x=sqrt(x+1)

4.     向上的箭头允许回调到以前给出的命令

5.    matlab在执行过程中保持16位有效数字,但只显示5位有效数字

如果给出下面的命令:

>>format long

就会显示出16位有效数字

>>format short

就会显示出5位有效数字

6.    >> x=-1:.02:4

语句可以生成一个从-1出发,到4结束且步距为0.02的x向量                      

x =

  Columns 1 through 3

  -1.00000000000000  -0.98000000000000  -0.96000000000000

  Columns 4 through 6

  -0.94000000000000  -0.92000000000000  -0.90000000000000

7.    >> phi=(1+sqrt(5))/2

phi =

   1.61803398874989

>> y1=x;y2=sqrt(x+1);

>> plot(x,y1,'-',x,y2,'-',phi,phi,'o')   绘制有三个元素的图片

plot是绘制图片的函数PLOT(X,Y,S)S的值为:

b     blue          .    point              -     solid

g     green        o    circle             :     dotted

r     red            x     x-mark           -.   dashdot

c     cyan          +    plus               --    dashed  

m    magenta   *     star              (none)  no line

y      yellow      s    square

k      black        d    diamond

                       v     triangle (down)

                       ^     triangle (up)

                       <     triangle (left)

                       >     triangle (right)

                       p     pentagram

                              h     hexagram

For example, PLOT(X,Y,'c+:') plots acyan dotted line with a plus

at each data point; PLOT(X,Y,'bd')plots blue diamond at each data

point but does not draw any line.

 

PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...)combines the plots defined by

the (X,Y,S) triples,where the X's and Y's are vectors or matrices

       and the S's are strings. 

 

 

>> help  plot

 

 -- plot (Y)

 -- plot (X, Y)

 -- plot (X, Y, FMT)

 -- plot (..., PROPERTY,VALUE, ...)

 -- plot (X1, Y1, ..., XN, YN)

 -- plot (HAX, ...)

 -- H = plot (...)

     Produce 2-D plots.

 

     Many differentcombinations of arguments are possible. The

     simplest form is

 

          plot (Y)

 

     where the argument istaken as the set of Y coordinates and the X

     coordinates are taken tobe the range `1:numel (Y)'.

 

     If more than oneargument is given, they are interpreted as

 

          plot (Y, PROPERTY,VALUE, ...)

 

     or

 

          plot (X, Y,PROPERTY, VALUE, ...)

 

     or

 

          plot (X, Y, FMT,...)

 

     and so on.  Any number of argument sets may appear.The Xand Y

     values are interpreted asfollows:

 

        * If a single dataargument is supplied, it is taken as the set

          of Y coordinates andthe X coordinates are taken to be the

          indices of theelements, starting with 1.

 

        * If X and Y arescalars, a single point is plotted.

 

        * `squeeze()' isapplied to arguments with more th-- less -- (f)orward, (ban two

          dimensions, but nomore than two singleton dimensions.

 

        * If both argumentsare vectors, the elements of Y are plotted

          versus the elementsof X.

 

        * If X is a vector andY is a matrix, then the columns (or

          rows) of Y areplotted versus X.  (using whichever

          combination matches,with columns tried first.)

 

        * If the X is a matrixand Y is a vector, Y is plotted versus

          the columns (orrows) of X.  (using whichever combination

          matches, withcolumns tried first.)

 

        * If both argumentsare matrices, the columns of Y are plotted

          versus the columnsof X.  In this case, both matrices must

          have the same numberof rows and columns and no attempt is

          made to transposethe arguments to make the number of rows

          match.

 

     Multipleproperty-value pairs may be specified, but they must

     appear in pairs.  These arguments are applied to the lineobjects

     drawn by `plot'.  Useful properties to modify are"linestyle",

     "linewidth","color", "marker", "markersize","markeredgecolor",

     "markerfacecolor".  *Note Line Properties::.

 

     The FMT format argumentcan also be used to control the plot style.

     It is a string composedof four optional parts:

    "<linestyle><marker><color><;displayname;>".  When a marker is

     specified, but nolinestyle, only the markers are plo-- less -- (f)orward, (btted.

     Similarly, if a linestyleis specified, but no marker, then only

     lines are drawn.  If both are specified then lines and markerswill

     be plotted.  If no FMT and no PROPERTY/VALUE pairs aregiven, then

     the default plot style issolid lines with no markers and the

     color determined by the"colororder" property of the current axes.

 

     Format arguments:

 

    linestyle

          `-'  Use solid lines (default).

          `--' Use dashed lines.

          `:'  Use dotted lines.

          `-.' Use dash-dottedlines.

 

    marker

          `+'  crosshair

          `o'  circle

          `*'  star

          `.'  point

          `x'  cross

          `s'  square

          `d'  diamond

          `^'  upward-facing triangle

          `v'  downward-facing triangle

          `>'  right-facing triangle

          `<'  left-facing triangle

          `p'  pentagram

          `h'  hexagram

 

    color

          `k'  blacK

          `r' Red

          `g'  Green

          `b'  Blue

          `y'  Yellow

          `m'  Magenta

          `c'  Cyan

          `w'  White

 

    ";displayname;"

          Here"displayname" is the label to use for the plot legend.

 

     The FMT argument may alsobe used to assign legend labels.  To do

     so, include the desiredlabel between semicolons after the

     formatting sequencedescribed above, e.g., "+b;Key Title;".  Note

     that the last semicolonis required and Octave will generate an

     error if it is left out.

 

     Here are some plotexamples:

 

          plot (x, y,"or", x, y2, x, y3, "m", x, y4, "+")

 

     This commandwill plot `y' with red circles, `y2' with solid

     lines, `y3' with solidmagenta lines, and `y4' with points

     displayed as `+'.

 

          plot (b,"*", "markersize", 10)

 

     This commandwill plot the data in the variable `b', with points

     displayed as `*' and amarker size of 10.

 

          t = 0:0.1:6.3;

          plot (t, cos(t),"-;cos(t);", t, sin(t), "-b;sin(t);");

 

     This will plotthe cosine and sine functions and label them

     accordingly in thelegend.

 

     If the firstargument HAX is an axes handle, then plot into this

     axis, rather than thecurrent axes returned by `gca'.

 

     The optionalreturn value H is a vector of graphics handles to the

     created line objects.

 

     To save a plot, in one ofseveral image formats such -- less -- (f)orward, (bas PostScript

     or PNG, use the `print'command.

 

8.    for语句:

>>x=3,for k=1:31,x=sqrt(x+1),end

Syntax

 

for variable = expression

    statements

end

Description

 

The general format is

for variable = expression

    statement

    ...

    statement

end

for语句因为知道循环的次数,所以for后面跟的是循环的次数,用逗号分隔,用end结束

 

>> help for

 

 -- forI = RANGE

    Begin a for loop.

 

         for i = 1:10

           i

         endfor

 

    See also: do, parfor, while.

 

 

 

>> help do

 

 -- do

    Begin a do-until loop.  Thisdiffers from a do-while loop in that

    the body of the loop is executed at least once.

 

         i = 0;

         do

           i++

         until (i == 10)

 

    See also: for, until, while.

 

 

 

 

 

>> help parfor

 

 --parfor I = RANGE

 --parfor (I = RANGE, MAXPROC)

    Begin a for loop that may execute in parallel.

 

         parfor i = 1:10

           i

         endparfor

 

    See also: for, do, while.

 

 

 

9.    while语句:

>> whilex~=sqrt(x+1)

x=sqrt(x+1)

end

while语句因为不知道循环的次数,所以跟的是逻辑表达式,while语句所在的第一行与其他语句分开,不可用逗号分隔。

 

 

>> help while

 

 -- while

     Begin awhile loop.

 

          i =0;

         while (i < 10)

           i++

         endwhile

 

     See also: do, endwhile, for, until.

 

10.  eps(x)表示在x附近的浮点数精度误差线

11.  helpsqrt,help for

提供简洁的帮助

12.  doc sqrt,docfor

会在单独的窗口上提供更详细的信息

13.  https://www.zhihu.com/question/22808635/answer/122479201

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值