版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、MATLAB 結構化財務程式之撰寫MATLAB財務程式實作應用研習 主題五資管所 陳竑廷MATLAB 結構化財務程式之撰寫MATLAB財務程式實作應2大綱非循序指令決策、選擇迴圈、反覆巢狀結構與內縮向量化2大綱非循序指令3一、非循序指令循序 (Sequencing)根據code由上而下循序逐行執行每一個指令非循序選擇反覆3一、非循序指令循序 (Sequencing)4Selectionif提款金額確認switch功能選擇4Selectionif5ifif 邏輯條件一運算指令一elseif 邏輯條件二運算指令二elseif 邏輯條件三運算指令三else運算指令endS=55標的物價格X=60 履
2、約價格if SXdisp(價內賣權)elseif S=Xdisp(價平賣權)elsedisp(價外賣權)end5ifif 邏輯條件一S=55標的物價格6ifS=55標的物價格X=60 履約價格if SXdisp(價內賣權)elseif S60disp(S60)elseif S=Xdisp(價平賣權)elsedisp(價外賣權)end6ifS=55標的物價格7error函數語法 error(message)當遇到此函數時,MATLAB會以紅色字體顯示message,此時M檔將停止執行,MATLAB回到命令視窗7error函數語法8error函數S=input(請輸入標的物價格);if S0, e
3、rror(價格須為正值),end8error函數S=input(請輸入標的物價格);9switchswitch 評估描述子(整數或字串)case 數值(或字串)條件一運算指令一case 數值(或字串)條件二運算指令二otherwise運算指令endswitch colorcase(red)signal=景氣過熱case(green)signal=景氣穩定case(blue)signal=景氣略冷otherwisesignal=景氣衰退end9switchswitch 評估描述子(整數或字串)swit10switch colorcase(red)signal=景氣過熱case(green)sig
4、nal=景氣穩定case(blue)signal=景氣略冷otherwisesignal=景氣衰退endswitch10switch colorswitch11Repetitionfor進行指定次數的重複動作之後停止。次數已定義while在某邏輯條件不成立時,才停止執行重複動作。次數未定義11Repetitionfor12forfor index = start : increment : endstatementsendfor dt = 2 : 1 : 8disp()end12forfor index = start : incre13for 倒數for index = start : in
5、crement : endstatementsendfor dt = 4 : -1 : 2disp()end13for 倒數for index = start : in14PreallocationMatlab stores matrices in contiguous blocks of memory. When the size of a matrix changes, Matlab, if it has not preallocated enough space, must find a new chunk of memory large enough and copy the matr
6、ix over. When a matrix grows inside of a loop, this process may have to be repeated over and over again causing huge delays. It can therefore significantly speed up your code by preallocating a chunk of memory before entering into a loop. 14PreallocationMatlab stores m15Preallocationticfor i = 1:300
7、00 A(i) = i;endwithout = tocticB = zeros(30000,1); % Preallocate B with the zeros command.for i = 1:30000 B(i) = i;endwith = tocratio = without / with15Preallocationtictic16whilewhile conditionstatementsendwhile pwd != password pwd = input(密碼:);endwhile其邏輯條件判定為 false 時,程式才會跳出迴圈。16whilewhile conditio
8、nwhile pw17whilebreakwhilebreak可使程式在某一個邏輯條件為 true 的情況下從迴圈跳出。white condition(1)statements(A)if condition(2), break , endstatements(B)end17whilebreakwhilebreak可使程式在某18whilebreakS %目前股價min_p = 20%停損點Max_p = 40%停利點while S Max_pif S min_p , break , endKeep_Stock()endSell_Stock()18whilebreakS %目前股價19二、巢狀結
9、構與內縮Nesting and Indentation程式中的結構可以築巢於程式中另外的結構之中巢狀結構:forforendend19二、巢狀結構與內縮Nesting and Indenta20二、巢狀結構與內縮for i = 1 : 1 : 2for j = 1 : 1 : 9 disp(i*j) endend20二、巢狀結構與內縮for i = 1 : 1 : 221三、VectorizationMatlab is an interpreted language, which means that each line of code must be reduced to machine i
10、nstructions as the program runs, whereas with compiled code, this is done before execution. But where Matlab is at a disadvantage, is with regard to loops. Although recent versions have seen a considerable increase in speed, loops are still a major bottleneck.We can frequently replace loops with mat
11、rix operations or calls to fast, built in functions - a process called vectorization. 21三、VectorizationMatlab is an22Non-vectorizedA = rand(200,200); tic Bnv = zeros(size(A); for i=1:size(A,1) for j=1:size(A,2); Bnv(i,j) = log(A(i,j); endendnonvec = tocVectorizedA = rand(200,200); tic Bv = log(A); vec = toc; 22Non-vectorizedVectorized23Non-vectorized versionVectorized version23Non-vectorized vers
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生物标志物在药物临床试验中的个体化治疗策略
- 生物墨水的细胞粘附性调控策略-1
- 县委关于2025年度“第一议题”制度落实情况的报告
- 生物制品稳定性试验光谱分析方法
- 生物信息学在基因治疗临床决策中的支持
- 深度解析(2026)《GBT 20063.15-2009简图用图形符号 第15部分:安装图和网络图》(2026年)深度解析
- 资金会计笔试考试题库含答案
- 深度解析(2026)《GBT 19448.6-2004圆柱柄刀夹 第6部分装圆柱柄刀具的E型刀夹》
- 英语教师面试题及英语教学经验
- 招聘面试题目及参考答案集
- 2026元旦主题晚会倒计时快闪
- 物理试卷答案浙江省9+1高中联盟2025学年第一学期高三年级期中考试(11.19-11.21)
- 俄语口语课件
- 2025广西自然资源职业技术学院下半年招聘工作人员150人(公共基础知识)综合能力测试题带答案解析
- django基于Hadoop的黑龙江旅游景点系统-论文11936字
- 电大专科《管理英语1》历年期末考试试题及答案汇编
- 《非政府组织管理》教学大纲
- GB/T 19809-2005塑料管材和管件聚乙烯(PE)管材/管材或管材/管件热熔对接组件的制备
- 无机及分析化学考试题(附答案)
- 体质中医基础理论课件
- 电力工程检验批质量验收记录【完整版】
评论
0/150
提交评论