- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
The great advantage of an interactive system is that programs can be tested and debugged quickly, allowing the user to concentrate more on the principles behind the program and less on programming itself. Since there is no need to compile, link and execute after each correction, Matlab programs can be developed in much shorter time than equivalent Fortran or C programs. for 循环变量 = 取值列表 循环体 end 取值列表 通常是一个向量 将取值列表中的值依次赋给循环变量, 直到全部取完, 循环结束 不要在循环体内改变循环变量的值 为提高代码的运行效率, 应尽可能提高代码的向量化程度 for 循环 y=0; n=100; for k=1:n y=y+1/(k^2); end 练习 已知 , 当 n=100时,求 y 的值。 循环语句可以嵌套使用 如果预先知道循环的次数,可采用 for 循环 如果预先无法确定循环次数,则可使用 while 循环 while 循环 while 条件表达式 循环体语句 end 当条件表达式的值为真(非 0)时, 执行循环体语句, 直到条件表达式的值是假为止 while 循环 表达式 循环体语句 真 假 yp=1; y=1.25; n=3; while abs(y-yp)10^(-5) yp=y; y=y+1/(n^2); n=n+1; end 已知 , 估计序列yn的极限 思考:请体会for和while的异同? 练习 A computation is vectorized by taking advantage of vector operations. A variety of programming situations can be vectorized, and often improving speed to 10 times faster or even better. Vectorization is one of the most general and effective techniques for writing fast M-code. Writting Fast Matlab Codes(向量化是高效Matlab代码的核心) 练习 如何计算矩阵元素的平方? n=10 A=randn(n,n); tic for i= 1:n for j=1:n A(i,j)=A(i,j)^2 end end t1=toc tic,A=A.^2;t2=toc 构造如下4行4列的矩阵 练习 for i=1:4 for j=1:4 A(i,j)=1/(i+j-1); end end 思考:上述代码是否可以向量化? 编写一个大程序的最好的方法是将它以好的设计分化为小块(通常采用函数的方式)。这种方式通过减少为了理解代码的作用而必须阅读的代码数量使得程序的可读性、易于理解性和可测试性得到了增强。超过编辑器两屏幕的代码都应该考虑进行分割。并且设计规划很好的函数也使得它在其他的应用中可用性增强了。 We attempt to place emphasis on numerical methods, not programming. We are not programmers, but problem solvers. We want to know what methods can be applied to a given problem, what are their strengths and pitfalls and how to implement them. We are not expected to write computer code for basic tasks from scratch; We are more likely to utilize
原创力文档


文档评论(0)