第5讲Matlab程序设计.ppt
《第5讲Matlab程序设计.ppt》由会员分享,可在线阅读,更多相关《第5讲Matlab程序设计.ppt(81页珍藏版)》请在第壹文秘上搜索。
1、第五讲 MatlabMatlab程序设计课程提纲 引子引子 M M文件介绍文件介绍 控制语句控制语句 函数变量及变量作用域函数变量及变量作用域 程序设计的辅助函数程序设计的辅助函数 程序设计的优化程序设计的优化 程序调试程序调试 信息接口信息接口 小结小结page2引子华氏温度和摄氏温度的转换Problem: Problem: 设计一个设计一个MATLAB MATLAB 程序,读取一个华氏温度的程序,读取一个华氏温度的输入,输出摄氏温度。输入,输出摄氏温度。Solution:Solution:Step 1: Step 1: 清晰地陈述出你要解决的问题;清晰地陈述出你要解决的问题; “将华氏温度
2、转换为摄氏温度并输出将华氏温度转换为摄氏温度并输出”Step 2: Step 2: 确定程序的输入变量和输出变量;确定程序的输入变量和输出变量; “输入变量输入变量:华氏温度;:华氏温度;输出变量输出变量:摄氏温度:摄氏温度”Step 3: Step 3: 设计程序伪代码;设计程序伪代码; % % 读取华氏温度:读取华氏温度:input input T T(F)(F) % % 温度转换:温度转换: % % 输出摄氏温度:输出摄氏温度:print print T T(C)(C)5(C)( (F)32)9TTpagepage3 3Step 4: Step 4: 将伪代码转换为将伪代码转换为Matl
3、abMatlab程序语句程序语句% Script file:temp_conversion.m% Script file:temp_conversion.m% % Purpose:% Purpose:% To convert an input temperature from degrees Fahrenheit to% To convert an input temperature from degrees Fahrenheit to% an output temperature in Celsius.% an output temperature in Celsius.% % Record
4、 of revisions:% Record of revisions:% Date Programmer Description of change% Date Programmer Description of change% = = =% = = =% 21/10/13 Y.Y.Guo Original code% 21/10/13 Y.Y.Guo Original code% % Define variables:% Define variables:% temp_f -Temperature in degrees Fahrenheit% temp_f -Temperature in
5、degrees Fahrenheit% temp_k -Temperature in Celsius% temp_k -Temperature in Celsius% Prompt the user for the input temperature.% Prompt the user for the input temperature.temp_f=input(Enter the temperature in degrees Fahrenheit:);temp_f=input(Enter the temperature in degrees Fahrenheit:);% Convert to
6、 Celsius.% Convert to Celsius.temp_k=(5/9)temp_k=(5/9)* *(temp_f-32);(temp_f-32);% Write out the result.% Write out the result.fprintf(%6.2f degrees Fahrenheit = %6.2f fprintf(%6.2f degrees Fahrenheit = %6.2f Celsius.n,temp_f,temp_k);Celsius.n,temp_f,temp_k);“程序信息程序信息”“名字名字”“用途用途”“修改记录修改记录”“变量定义变量定义
7、”“读取华氏温度读取华氏温度”“温度转换温度转换”“输出摄氏温度输出摄氏温度”pagepage4 4Step 5: Step 5: 调试程序调试程序 run(C:Program FilesMATLABR2010abintemp_conversion.m) run(C:Program FilesMATLABR2010abintemp_conversion.m)Enter the temperature in degrees Fahrenheit:120Enter the temperature in degrees Fahrenheit:120120.00 degrees Fahrenheit
8、= 48.89 Celsius.120.00 degrees Fahrenheit = 48.89 Celsius.Enter the temperature in degrees Fahrenheit:100Enter the temperature in degrees Fahrenheit:100100.00 degrees Fahrenheit = 37.78 Celsius.100.00 degrees Fahrenheit = 37.78 Celsius.MatlabMatlab初学者总是期望,拿到问题就能写出漂亮的代码与完初学者总是期望,拿到问题就能写出漂亮的代码与完美无误的程序
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Matlab 程序设计
