版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《2024年 绒山羊和绵羊Izumo1和CD9基因的克隆及其相互作用的初步研究》范文
- 《2024年 几种双钙钛矿氧化物薄膜-超晶格的结构与性质研究》范文
- 人教版五年级数学下册导学案《重点单元知识归纳与易错警示 (2)》
- 出版业数字化内容运营及推广策略
- 感恩母亲节电子小报
- 【人教版】二年级数学下册教学设计《第1课时 轴对称图形》
- 中国石油化工工程行业市场深度剖析与投资战略研究报告2024-2030年
- 《实验:游标卡尺和螺旋测微器》课件
- 2024年上半年郑州房地产市场回顾与展望-8正式版
- 解析:仁爱版英语八年级上册Unit 1 Topic 1 Section D 同步练习(原卷版)
- 全国各省份城市明细表
- 《气候数值模拟》全套教学课件
- 四年级上册科学13我国的珍稀动物教学课件
- 2023三季度手卫生报告
- 小学语文人教三年级上册(统编2023年更新)第一单元-有意思的拟人(终极)
- “原地双手头上正面掷实心球”学习任务单
- 房地产大运营管理体系构建
- 固定污染源排污登记表(样表)
- 英美合同法课件
- 公司催款函范文(精选19篇)
- 4AA培南中间体及培南类药物小结
评论
0/150
提交评论