, , xmin m, . - . , -, , . . , -. . - , - . , - MatLab.
3.4-1. . 3.4-1.
. 3.4-1. - 3.4-1
m- MatLab. , . :
m- |
function var=f_name(Cnco_napaep) % % m- vr= |
m- :
function, var - , ;
f _ name (_) ;
, -, , . . ;
- , ;
, -;
- MatLab;
- , MatLab.
v= , , .
- . , function. :
- |
function [varl,var2....]=f_name(_) % % v1= v2= |
. , , .
, , . . , , :
|
|
[var1,var2,... ]=f_nm(_)
varl, var2,... . f_nm(_), varl.
, , , .
- 3.2-3, .
3.4-2 |
function z=fun(x,y) % 5.3.2-2 z=x^2+y^2; |
3.4-2 |
>> z=fun(2,3); >> x= >> y= >>z= >> |
fun(x,y) , z = 2 + 2. fun(x, ), . . , fun(2, 3) =2 =3. - z=13. , . .
z ? , , , . . z=13. , 3.4-1, z, , . , z.
, . . . , , . return. , , , ( z), .
z=13 , z 0. z, ; , z.
, ( ;) . .
, - , - - . . .
|
|
, . - . , .
Globalvarl var2...
- . , , . , , global .
. , sin(x)/x - = 0 . . , , , . . , .
error(' ');
, , , . sd(x)=sin(x)/x, :
_3_4_3 |
function f=sd(x) ifx==0 error(' - 0'). end f=sin(x)/x; |
if, . :
3.4-3 |
>>x=0; >>f=sd(x); ??? - 0 >>x=1; >>f=sd(x); >> f= >> |
,
warning(' ').
, . , , -, ??? Warning .
:
nargin ;
nargout .
, , , xl, 2, , 4 5.
sum2_5:
sum2_5__3.4-4 |
function f=sum2_5(x1,x2,x3,x4,x5); f=x1^2+x2^2+x3^2+x4*2+x5^*2; |
3.4-4 |
sum2_5__5_3_2_4 (l,2,3,4,5) ans = sum2_5(l,2) ??? Input argument '' is undefined. Error in ==> C:\MATI_AB\bin\sum2_5.m On line 2 ==> f=x1^2+x2^2+x3^2+x4^2+x5^2; >> |
, . , . nargin sum2_5m, 1 5:
sum2_5__3_4_5 |
f unction f=sum2_5m(x1,x2, ,x4, x5); n=nargin; if n==1 f=x1^2; end if n==2 f=x1^2+x2^2;end if n==3 f=x1^2+x2^2+x3^2; end if n==4 f=x1^2+x2 ^ 2+x3^2+x4 ^ 2: end if n==5 f=x1^2+x2^2+x3^2+x 4^2+x5^2 |
if...end, . , nargin , , - . :
|
|
3.4-5 |
sum2_5m(1) ans = sum2_5m(1,2) ans = sum2_5m(1,2,3) ans = sum2_5m(1,2,3,4) ans = sum2_5m(1,2,3,4,5) ans= sum2_5m(1,2,3,4,5,6) ??? Error using ==> sum2_5m Too many input arguments. >> |
, 1 5 . . MatLab
, helpname, name m - , , . , , . .
helpname. typename , .
helpcatalog, catalog m - , , . contents.m, m - . , m- .
m - - , m - . . , . m - , , .
. , MatLab , , m - , PRIVATE . , .
, , . . () , , MatLab. . .
. Outofmemory.
:
, ;
Windows;
;
;
.
, MatLab . , 256 .
m-
MatLab m-, MatLab . /, . . m-, , . , . File/SetPath, .
|
|
m- MatLab , m- . , foo,atLab :
, foo ;
, foo ;
m - foo.m;
m - foo.m .
. m - , .
, :
path - ;
path(s) - s;
addpath/home/lib path(path, /home/lib) - ;
rmpath /home/lib - /home/lib .
, , pathdef.m, ll; MatLab.
, PathBrowser, .
MatLab m - . .
cd.
d< >.
, Windows SetPath (. 3.3-1). File/SetPath , .
File/SavePath, .
. 3.4-1. SetPath
/ m - , . MatLab edit. , editpoof poof.m, File Preferences .
File/New NewFile . m - File/Open OpenFile.
/ , . 3.4-2.
. 3.4-2. /
, , , .. , . :
.
Tools/Fonts , . - (Del, Bspace, Home ..). Edit/GoToLine . . , (.. ) Debug/Run. .
, , . , , . , . ( ) View/WorkspaceBrowser. View. EvaluateSelection, , AutoIndentSelection, MatLab. View/Options , .
|
|
- , :
, m- . MatLab , m-;
, , , , .
, . , m- , MatLab. , :
, , ;
m - keyboard, m - m-( ), return);
m- ( );
atLab.
, . , m - , . , m- m - . , .
, . , Editor/Debugger.
edit<_> File/Open. / File/New/M-file. /. . 3.2-2.
. , , , . . , (Stepin) -
(SingleStep), (Continue) (QuitDebugging). , View/WorkspaceBrowser , . , , , .
m-
MatLab m-, Newm-file , File New/m-file. . - , :
3.4-6 |
% % x=[-1:0.01:1]; y=exp(x); plot(x,y) gridon title(' ') |
. , , , <Shift> , <PageUp>,<PageDown> Text EvaluateSelection ( <F9>). , , . , m - , , , , , , .
, m - , , myprog.m, Run Debug, m- ( ) <Enter>, , MatLab. m - MatLab. myprog .
MatLab File SetPath PathBrowser ( ). CurrentDirectory . , , .
MatLab CurrentDirectory . , CurrentDirectory View . , .
, m - , , , m - . - , . . . , whos. , - . , :
- 3.4-7 |
>> a=[0.1 0.4 0.3 1.9 3.3]; >> |
-,
-_3_4_7 |
% % bar() |
bar(), a (, -).
- - , , , -, . m - , -, , MatLab. function, , - . . 3.4-3 - .
-_3_4_8 |
function c=mysum(a,b) c=a+b; |
. , MatLab m - -, .. mysum.m. - m - , -! , mysum.m - mysum :
3.4-8 |
>> s=mysum(2,3) s = >> |
- mysum :
a 2;
b 3;
a b c;
c s .
, c=a+b - mysum c . -, , .
MatLab - m- . sin : sin(x) y=sin(x), ans, y. mysum . , mysum .
, - . - , . 3.4.4-4 - quadeq, .
_3_4_9 |
function [x1,x2]=quadeq(a,b,c) % D=b^2-4*a*c; x1=(-b+sqrt(D))/(2*a); x2=(-b-sqrt(D))/(2*a); |
3.4-9 |
% quadeq % , : >> [r1,r2]=quadeq(1,3,2) r1 = -1 r2 = -2 >> |
, - quadeq , . .
- . - :
Functionnoout(a,b),
function [v,u]=noin,
Functionnoarg()
- - MatLab, MatLab ( , , , ). ,
y=exp(-x).*(sin(x)+0.1*sin(100*pi*x)) [0;1].
, myfun() .
_3.4-10 |
function y=myfun(x); % y=exp(-x).*(sin(x)+0.1*sin(100*pi*x)); % y . % % , 0.01, % plot: >> x=[0:0.01:1]; >> y=myfun(x); >> plot(x,y) >> |
3.4-10 |
%% y . % , 0.01, % plot: >> x=[0:0.01:1]; >> y=myfun(x); >> plot(x,y) >> |