MATLAB相关细节

版权声明:本文为作者原创,如需转载,请征得博主同意,谢谢! https://blog.csdn.net/qq_22828175/article/details/83413079
  1. 在MATLAB中,break 退出的是for、while两种循环语句,不对if、switch负责。
    ex: Find the root of the polynomial x^3 - 2x - 5 using interval bisection.
a = 0; fa = -Inf; 
b = 3; fb = Inf;
while b-a > eps*b
   x = (a+b)/2;
   fx = x^3-2*x-5;
   if fx == 0
      break
   elseif sign(fx) == sign(fa)
      a = x; fa = fx;
   else
      b = x; fb = fx;
   end
end
disp(x)

break关键字在python中同样是退出for、while两种循环语句,不对条件语句if负责。

  1. matlab和python中对局部变量赋初值是极为重要的,这样可以保证之前定义或计算出的同名变量的值不被重复调用。

  2. The hold function controls the current graph when you make subsequent calls to plotting functions(the default), or adds a new graph to the current graph.
    hold 似乎只对plot出来的图形起作用,对其他自动生成的图形不起作用。

  3. MATLAB中只有elseif语句,没有 else if 语句;python中是elif。

  4. 在写计算式的时候尽量写成简化形式,原形式在注释中写出便于理解意义。这样可以减少计算机的迭代次数,以减小传播误差。

  5. 全局变量global是存在matlab缓存中的不变量,在matlab任何地方均可调用,具有唯一性。除非重新计算覆盖了原来的值,否则其值一直不变,且会在出现的地方起作用。

  6. 对于要进行赋值的变量,可以不用事先定义;对于带索引的变量数组,应进行事先定义以提高运算速度。

  7. MATLAB只要打开,运行一次定义了global变量的程序,则在关闭MATLAB之前,无论怎样clear,都不会清空用global定义的变量,在任一路径的任一程序中,都可以调用global变量。相当于所有的global变量运行之后都会储存到matlab的缓存中,只有在关闭matlab时才会清除。可见clear清除的是局部变量。

  8. MATLAB stores numbers as floating-point values, and arithmetic operations are sensitive to small differences between the actual value and its floating-point representation.
    format affects only the display of numbers, not the way MATLAB computes or saves them.

猜你喜欢

转载自blog.csdn.net/qq_22828175/article/details/83413079