MATLAB
for
for count = start:step:final
MatLab
end
count ; start ; final ; step ( ) count. 1, .
, Q, , : , - hil :
------------------------------------------------------------------------------------------------------------------------
% Q
n = 4;
% Q
Q = zeros(n);
% Q
for i = 1:n
for j = 1:n
Q(i, j) = 1/(i + j - 1);
end
end
%
disp(' Q = ')
disp(Q)
------------------------------------------------------------------------------------------------------------------------
while
while
MatLab
end
:
= = | |
< (>) | () |
< = (> =) | ( ) |
~ = |
MatLab | |||
and (x<3, k = = 4) | (x<3) & (k = = 4) | ||
or (x = = 1, x = = 2) | (x = = 1) | (x = = 2) | ||
not (x = =2) | ~ (x = = 2) |
Q. , . M-. Ctrl+C Ctrl+Break.
- hil.
------------------------------------------------------------------------------------------------------------------------
s = 0;
k = 1;
while k <= n
s = s + Q(k, k);
k = k+1;
end
%
disp(' Q = ')
disp(s);
-----------------------------------------------------------------------------------------------------------------------
while , , for, k , k .
if
if
if
MatLab
end
, - Rfun
.
------------------------------------------------------------------------------------------------------------------------
function f = Rfun(x);
if abs(x) < 1
warning (' ')
end
f = sqrt(x^2-1);
-----------------------------------------------------------------------------------------------------------------------
|
|
warning .
Rfun , , :
>> y = Rfun(0.2)
Warning:
y =
0 + 0.9798i
if - elseif else
- ifdem, if - elseif else.
-----------------------------------------------------------------------------------------------------------------------
function ifdem(a);
if (a == 0)
disp('a ')
elseif a == 1
disp('a 1')
elseif a == 2
disp('a 2')
elseif a >= 3
disp('a 3')
else
disp('a 3, 0, 1, 2')
end
----------------------------------------------------------------------------------------------------------------------
, , , else.
ifdem 1.2:
>> ifdem(1.2)
a 3, 0, 1, 2
, , , . break. break , end.
, prch, N .
------------------------------------------------------------------------------------------------------------------------
N = input(' : ')
function prch(N);
% , (pr=1)
pr = 1;
% N 2 N/2
for i=2:N/2
% N i,
if mod(N,i) == 0
% N (pr=0)
pr =0;
%
break;
end
end
% pr 1, N -
if pr == 1
disp(' ')
else
disp(' ')
end
------------------------------------------------------------------------------------------------------------------------
, error.
, prch.
------------------------------------------------------------------------------------------------------------------------
N = input(' : ')
ost = round(N)- N;
if (N > 0) & (ost == 0)
% , (pr=1)
pr = 1;
else
error(' N ')
end
% N 2 N/2
........
------------------------------------------------------------------------------------------------------------------------
, , , .
4. switch
switch,
switch
case 1
1
case 2
2
case 3
3
|
|
....
otherwise
end
1, 1, , 2, 2 .. case, , otherwise.
, , D, 31 1- .
------------------------------------------------------------------------------------------------------------------------
D = input(' 1 31: ')
function day_week(D)
md = mod(D,7); % D 7
switch md
case 1
disp('')
case 2
disp('')
case 3
disp('')
case 4
disp('')
case 5
disp('')
case 6
disp('')
otherwise
disp('')
end
------------------------------------------------------------------------------------------------------------------------
MatLab -, , , -. - , feval.
, -, .
------------------------------------------------------------------------------------------------------------------------
function str = znach (fname, ymin, ymax, x)
%
if ymin >= ymax
error(' ')
end
y = feval(fname, x)
if (y >= ymin) & (y <= ymax)
str = ' ';
else
str = ' ';
end
------------------------------------------------------------------------------------------------------------------------
. str .
MatLab, , .
, :
>> r = znach('sin', 0.2, 0.5, pi/5)
y =
0.58779
:
1. .
------------------------------------------------------------------------------------------------------------------------
% :
a = -2.5;
b = 1.32;
Fi = 3.89e-3;
A = [1.2 3.2 4.5; 2.3 7.8 4.4];
.
.
.
-----------------------------------------------------------------------------------------------------------------------
2. input . M-
------------------------------------------------------------------------------------------------------------------------
a = input(' a = ')
%
.
.
------------------------------------------------------------------------------------------------------------------------
,
a = |
a.
.
1. disp.
------------------------------------------------------------------------------------------------------------------------
.
.
.
disp(' x = ')
disp(x)
------------------------------------------------------------------------------------------------------------------------
|
|
, .
2. fprintf . fprintf :
fprintf('', ).
, , :
------------------------------------------------------------------------------------------------------------------------
x = pi/4;
y = sin(x);
fprintf('x = %7.4f, y = %11.8f\n', x, y)
------------------------------------------------------------------------------------------------------------------------
,
x = 0.7854, y = 0.70710678
>>
%7.4f x, . , 7 , x 7 ( ), 4 - ─ . f , . %11.8f y.
%d.
\n .
fprintf . , sin(x).
------------------------------------------------------------------------------------------------------------------------
clear all
clc
%
fprintf(' sin(x)\n')
fprintf(' -----------------------------\n')
fprintf(' | x | y |\n')
fprintf(' -----------------------------\n')
%
x = 0:pi/4:2*pi;
% ,
% -
M = [x; sin(x)];
fprintf(' | %8.5f | %8.5f |\n', M)
fprintf(' -----------------------------\n')
------------------------------------------------------------------------------------------------------------------------