【问题标题】:Matlab tabling syntaxMatlab 表格语法
【发布时间】:2013-10-06 04:02:20
【问题描述】:

matlab 新手,有些代码我听不懂:

x = 1; % initial guess = 1 
Tol = 5e-9; % correct to 8 decimal places 
count = 0; 
f=0.54030231; % f(1)= 0.54030231 
fprintf('step x f(x)\n') 
fprintf('---- ----------- ----------\n') 
fprintf('%1i %12.8f %12.8f\n',count,x,f) 
while abs(f)>Tol %loop until the absolute value of f is smaller than tolerance
count = count + 1 
deriv = -sin(x); ; % first derivative of f(x) 
x2 = x - (f/deriv); % new value of x 
x = x2; 
f = cos (x); % new value of f(x) 
fprintf('%3i %12.8f %12.8f\n',count,x,f) 
end

该程序是牛顿法,用于查找我理解的方程的根。

我不明白的是这部分:

fprintf('---- ----------- ----------\n') 
fprintf('%1i %12.8f %12.8f\n',count,x,f) 

问题:

  1. 为什么第二行的最后一位除以n?
  2. 第二行中的数字是什么,即 %1i、%12.8f 等?
  3. 这如何与后面写的“count, x, f”一起工作?

谢谢

【问题讨论】:

  • 你读过fprintf的文档吗?如果你没有,为什么不呢?如果有,你有什么不明白的?

标签: matlab printf format-string


【解决方案1】:

\n 是换行符。

对于其余部分,您正在查看 Matab 的 printf format strings 版本。稍有不同,它们被用于数十种语言。在%处插入count、x和f1i是1位整数,12.8f是12个字符的浮点数,小数点后8位。

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多