Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB Script Files: An Introduction, Study notes of Engineering

An overview of matlab script files, their creation, execution, and access to variables. It includes examples of simple statistics and taylor series scripts. Script files are ascii files containing matlab commands, saved with a .m extension. They can call built-in functions, other scripts, or matlab function files. All variables in the matlab workspace are accessible to the commands in the script file, and all variables created in the script file are stored in the matlab workspace. Script files are executed by typing the file name (without the extension) at the matlab prompt. Examples of script files for simple statistics and taylor series.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-3ek-2
koofers-user-3ek-2 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB Script Files: An Introduction and more Study notes Engineering in PDF only on Docsity! 1 Chapter 4 Script M-files Script files are a sequence of MATLAB commands stored as a separate program executable from the MATLAB workspace. They are ascii files created with a text editor and saved with a .m extension file name. Script files may contain calls to built- in functions, other script files or MATLAB function files. It is important to remember that all variables present in the MATLAB workspace, including those remaining from a previously executed program, are accessible to the commands in the script file and all variables created in the script file are stored in the MATLAB workspace. Script files are executed by typing in the name of the file (without the extension) at the MATLAB prompt in the Command window. The individual commands comprising the script file can be displayed by typing 'echo on' prior to running the script file or by including the command 'echo on' inside the script file. The script file in Example 4.1 below would be entered a line at a time in the MATLAB Editor/Debugger window and then saved as 'Simple_Statistics' . MATLAB will provide the .m extension designating it as a m-File. To run it, simply type the script file name 'Simple_Statistics' in the Command window. Example 4.1 % Script file Simple_Statistics.m % This file generates a vector of n random numbers uniformly % distributed between 0 and 1. The numbers are transformed into % uniformly distributed numbers over a new interval [a,b]. The built- % in MATLAB functions 'mean' and 'std' for the sample mean and standard % deviation are called and the values are compared to the theoretical % values. n=500; % Set sample size x=rand(1,n); % Generate n U(0,1) random numbers a=10; % Transformed lower limit b=20; % Transformed upper limit x=(b-a)*x+a; % Transformation from U(0,1) to U(a,b) ave=mean(x); % Find mean sample std_dev=std(x); % Find sample std deviation ave_theoretical=(a+b)/2; % Compute theoretical mean std_dev_theoretical=(b-a)/sqrt(12); % Compute theoretical std deviation n,ave,std_dev,ave_theoretical,std_dev_theoretical n = 500 ave = 15.0722 std_dev = 2.7907 ave_theoretical = 15 std_dev_theoretical = 2.8868 2 Example 4.2 % Script file TaylorSeries.m % This file plots the nth order truncated Taylor Series of f(x)=cos(x). % The truncated series fn(x),n=0,..,10 are expanded about the point x0. % All graphs including f(x)=cos(x) are plotted from 0 to 2*pi radians. clear clc x0=0; x=0:0.01:2*pi; v=[0 2*pi -2 2]; z=zeros(size(x)); plot(x,z,'k') axis(v) hold on fx=cos(x); plot(x,fx,'k','Linewidth',1.5) xlabel('x (rad)') ylabel('f(x)=cos(x) and fn(x)') title('f(x)=cos(x) and nth Order Truncated Series (n=0,1,2,4,6,8,10)') axis manual fx0=cos(x0); plot([x0 x0],[0 fx0],'k--') fp1=-sin(x0); fp2=-cos(x0); fp3=sin(x0); fp4=cos(x0); fp5=fp1; fp6=fp2; fp7=fp3; fp8=fp4; fp9=fp5; fp10=fp6; f0=fx0*ones(size(x)); f1=f0+fp1*(x-x0); f2=f1+(fp2/2)*(x-x0).^2; f3=f2+(fp3/6)*(x-x0).^3; f4=f3+(fp4/24)*(x-x0).^4; f5=f4+(fp5/120)*(x-x0).^5; f6=f5+(fp6/720)*(x-x0).^6; f7=f6+(fp7/5040)*(x-x0).^7; f8=f7+(fp8/40320)*(x-x0).^8; f9=f8+(fp9/362880)*(x-x0).^9; f10=f9+(fp10/3628800)*(x-x0).^10; plot(x,f0,'b') plot(x,f1,'Color',[0.75 0.5 0.25]) plot(x,f2,'g') plot(x,f4,'r') plot(x,f6,'c') plot(x,f8,'m') plot(x,f10,'Color',[0.25 0.5 0.75])
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved