[第05章]MATLAB仿真技术及应用.pptVIP

  • 1
  • 0
  • 约7.41千字
  • 约 40页
  • 2019-05-26 发布于江西
  • 举报
MATLAB仿真技术及应用 Specifier Description %c Single character %d Decimal notation符号(signed) %e Exponential notation (lowercase小写 e as in 3.1415e+00) %E Exponential notation (uppercase大写 E as in 3.1415E+00) %f Fixed-point notation %g The more compact简洁的of %e or %f %G Same as %g, but using an uppercase大写 E %i Decimal notation (signed) %o Octal八进制notation (unsigned) %s String of characters %u Decimal notation (unsigned) %x Hexadecimal 十六进制notation (using lowercase letters a-f) %X Hexadecimal notation (using uppercase letters A-F) s=0; a=[1 2 3;4 5 6;7 8 9]; for k=a s=s+k; end disp(s’) a = 1 2 3 4 5 6 7 8 9 for语句应用示例: 6 15 24 2.while语句 while (条件) 循环体语句 end 若条件成立,则执行循环体语句,执行后再判断条件是否成立,如果不成立则跳出循环。 表达式为数组时全非0为真。 注意! a=[1,3,5;–1,0,1]; n=0; while a0    n=n+1 end 例: 由于a中有小于0的元素,故一次循环也未执行。 sum=0; cnt=0; val=input(Enter a number (end in 0):); while (val~=0) sum=sum+val; cnt=cnt+1; val=input(Enter a number (end in 0):); end if (cnt 0) sum mean=sum/cnt end 例:从键盘输入若干个数,当输入0时结束输入,求这些数的平均值和它们之和。 (见M文件sum_mean) %说明 清除命令:清除workspace中的变量和图形(clear,close) 定义变量:包括全局变量的声明及参数值的设定 逐行执行命令:指MATLAB提供的运算指令或工具箱 … … … 提供的专用命令 控制循环 :包含for,if,switch,while等语句 逐行执行命令 … … … end 绘图命令:将运算结果绘制出来 MATLAB程序的基本组成结构 5.3 完整的程序示例 示例:Lagrange插值法 Lagrange插值法的原理:已知函数f(x)的n+1个值f(x0), f(x1),…,f(xn),则Lagrange插值公式是: L(x)=f(x0)l0(x)+ f(x1)l1(x)+….+ f(xn)ln(x) 其中: 例:以f(x)=5ex+2log(x)为原函数,节点x1=1,x2=1.02,x3=1.04,x4=1.06,求f(x)的Lagrange插值多项式。 function r=f(x) r=5*exp(x)+2*log(x) ; %定义原函数 (1)函数文件f.m (2) 求Lagrange插值函数文件lagrange.m function s=lagrange(x) %定义插值函数 t=[1 1.02 1.04 1.06]; %给出的已知节点 n=4; %确定插值多项式的次数 s=0; for i=1:n; L(i)=1; for j=1:n; if j~=i L(i)=L(i)*(x-t(j))/(t(i)-t(j)); end end s=s+f(t(i))*L(i); %

文档评论(0)

1亿VIP精品文档

相关文档