MATLAB练习例子十一(M文件编辑)

返回

Logical Expressions Using the find Function 》 A = magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 》 i = find(A>8); 》 A(i) = 100 A = 100 2 3 100 5 100 100 8 100 7 6 100 4 100 100 1 logical operators 》 u = [1 0 2 3 0 5]; 》 v = [5 6 1 0 0 7]; 》 u & v ans = 1 0 1 0 0 1 》 u | v ans = 1 1 1 1 0 1 》 ~u ans = 0 1 0 0 1 0 》 a = 1; 》 b = 1; 》 xor(a,b) ans = 0 》 A = [0 1 2;3 5 0] A = 0 1 2 3 5 0 》 all(A) ans = 0 1 0 》 v = [5 0 8]; 》 any(v) ans = 1 》 》 A = magic(3) A = 8 1 6 3 5 7 4 9 2 》 3 * A ans = 24 3 18 9 15 21 12 27 6 Relational operators 》 A = [2 7 6; 9 0 5; 3 0.5 6]; 》 B = [8 7 0; 3 2 5; 4 -1 7]; 》 A == B ans = 0 1 0 0 0 1 0 0 0 》 A == [] Warning: X == [] is technically incorrect. Use isempty(X) instead. ans = 0 》 isempty(A) ans = 0 》 一些常用变量 》 eps ans = 2.2204e-016 》 realmax ans = 1.7977e+308 》 realmin ans = 2.2251e-308 》 pi ans = 3.1416 》 computer ans = PCWIN 》 version 》 x = 2*pi; 》 x x = 6.2832 》 A = [3+2i 7-8i] A = 3.0000 + 2.0000i 7.0000 - 8.0000i 》 tol = 3*eps tol = 6.6613e-016 》 function yp = lotka(t,y) %LOTKA Lotka-Volterra predator-prey model. global ALPHA BETA yp = [y(1) - ALPHA*y(1)*y(2); -y(2) + BETA*y(1)*y(2)]; 》 global ALPHA BETA 》 ALPHA = 0.01 ALPHA = 0.0100 》 BETA = 0.02 BETA = 0.0200 》 [t,y] = ode23('m0_lotka',0,10,[1;1]); Warning: Obsolete syntax. ode23("m0_lotka",tspan,y0,...)を代わりに使ってください > In C:\MATLAB\toolbox\matlab\funfun\ode23.m at line 100 》 plot(t,y) function m005(varargin) for i = 1:length(varargin) x(i) = varargin{i}(1); % Cell array indexing y(i) = varargin{i}(2); end xmin = min(0,min(x)); ymin = min(0,min(y)); axis([xmin fix(max(x))+3 ymin fix(max(y))+3]) plot(x,y) 》 m005([2 3],[1 5],[4 8],[6,5],[2,4]) function c = m004(a,b) % m004.m if (nargin == 1) c = a.^2; elseif (nargin == 2) c = a + b; end 》 m004 2 ans = 2500 》 m004 1 ans = 2401 》 m004 2 2 ans = 100 function y = average(x) % AVERAGE Mean of vector elements. m003.m % AVERAGE(X), where X is a vector, is the mean of vector elements. % Non-vector input results in an error. [m,n] = size(x); if (~((m == 1 ) | (n == 1)) | (m == 1 & n == 1)) error('Input must be a vector') end y = sum(x)/length(x); 》 z = 1:99; 》 m003(z) ans = 50 function c = m001(a,b) c = sqrt((a.^2)+(b.^2)) 》 a = 7.5 a = 7.5000 》 b = 3.342 b = 3.3420 》 c = m001(a,b) c = 8.2109 c = 8.2109 》 列出文件名 what/dir 显示M文件的内容(设存在file.m) type file % An M-file script to produce, m002.m % "flow petal" plots theta = -pi:0.01:pi; rho(1,:) = 2*sin(5*theta).^2; rho(2,:) = cos(10*theta).^3; rho(3,:) = sin(theta).^2; rho(4,:) = 5*sin(3.5*theta).^3; for i = 1:4 polar(theta,rho(i,:)) pause end 求[0,1]范围的n个随机数 n = input('Enter n '); x = rand(1,n) xmax = max(x) xmin = min(x) y = sort(x) 运行结果 Enter n 5 x = 0.6154 0.7919 0.9218 0.7382 0.1763 xmax = 0.9218 xmin = 0.1763 y = 0.1763 0.6154 0.7382 0.7919 0.9218 x = input('Enter x '); tf = input('Enter tf '); A = [0.8 1; 0 0.8] y = []; for i = 1:tf+1 y(:,i) = x; x = A*x; end y 》 test18 Enter x [1 1] Enter tf 2 A = 0.8000 1.0000 0 0.8000 ??? 割り当て A(:,matrix) = B では、A のサブスクリプトの要素数と B の列数が同じである必要があります エラー: ==> C:\test\test18.m 行番号: 7 ==> y(:,i) = x; ------------------------------------------------- 计算阶乘 n = input('Enter n '); y = 1; for i = 1:n y = y*i; end y n = input('Enter n '); x = 1:n y = prod(x) 进行累加的程序 n = input('Enter name '); s = 0; for i = 1:n s = s + i; end s 计算y=1+x+x2+...+xn x = input('Enter x '); n = input('Enter n '); a = 1; y = 1; for i = 1:n a = a*x; y = y + a; end y Enter x 1.01 Enter n 100 y = 173.1862 user_entry = input('prompt', 's') returns the entered string as a text variable rather than as a variable name or numerical value. name1 = input('Enter name ','s'); 画面输出到文件中去 diary(outputf) -------------------------------------------- name1 = input('Enter name ','s'); output1 = '_out.txt'; outputf = strcat(name1,output1) diary(outputf) x11 = '_x11.txt'; fx11 = strcat(name1,x11); X11 = dlmread(fx11,';') x12 = '_x12.txt'; fx12 = strcat(name1,x12); X12 = dlmread(fx12,';') x21 = '_x21.txt'; fx21 = strcat(name1,x21); X21 = dlmread(fx21,';') x22 = '_x22.txt'; fx22 = strcat(name1,x22); X22 = dlmread(fx22,';') #计算逆矩阵 X22inv = inv(X22); X = X11 - X12 * X22inv * X21 diary off ------------------------------------- n = input('Enter n '); switch n case -1 disp('minus one'); case 0 disp('zero'); case 1 disp('one'); otherwise disp('other value'); end 》 test8 Enter n 1 one 》 test8 Enter n 0 zero 》 test8 Enter n -1 minus one 》 test8 Enter n 99 other value n = input('Enter n '); switch n case 1 disp('1'); case {2,3,4} disp('2 or 3 or 4'); case 5 disp('5'); otherwise disp('other value'); end 》 test9 Enter n 3 2 or 3 or 4 》 test9 Enter n 1 1 》 test9 Enter n 5 5 》 test9 Enter n 88 other value 》 eps = 0.001; a = input('Enter a '); while a >= eps a = a*0.5 end 》 test10 Enter a 2 a = 1 a = 0.5000 a = 0.2500 a = 0.1250 a = 0.0625 a = 0.0313 a = 0.0156 a = 0.0078 a = 0.0039 a = 0.0020 a = 9.7656e-004 》 test1.m A = input('Enter matrix A ') 》 test1 Enter matrix A [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 》 test2.m A = input('Enter matrix A ') B = input('Enter matrix B ') X = A + B Y = A - B 》 test2 Enter matrix A [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 Enter matrix B [1 -1 1; 2 2 -1; -1 2 1] B = 1 -1 1 2 2 -1 -1 2 1 X = 2 1 4 6 7 5 6 10 10 Y = 0 3 2 2 3 7 8 6 8 function sequence = collatz(n) sequence = n; next_value = n; while next_value > 1 if rem(next_value,2) == 0; next_value = next_value / 2; else next_value = 3*next_value + 1; end sequence = [sequence, next_value]; end function collatzplot(n) clf set(gcf,'DoubleBuffer','On') set(gca,'XScale','linear') for m = 1:n plot_seq = collatz(m); seq_length(m) = length(plot_seq); line(m,plot_seq,'Marker','.','MarkerSize',9,'Color','blue') drawnow end collatzplot(500)

描画sin曲线 t = 0:0.01:10; y = sin(t); plot(t,y) grid on 描画cos曲线 t = 0:0.01:10; y = cos(t); plot(t,y) grid on 描画tan曲线 t = 0:0.01:10; y = tan(t); plot(t,y) grid on 描画一个sin曲线 t = 0:5:300; y = 0.5*sin(t*pi/300); plot(t,y) grid on 描画一个cosech曲线 t = 5:5:295; y = 0.5*csch(t*pi/300); plot(t,y) grid on



x = input('Enter x '); if x > 0 y = 1; elseif x<0 y = -1; else y = 0; end y 》 test7 Enter x 0 y = 0 》 test7 Enter x 3 y = 1 》 test7 Enter x -2 y = -1 》 ------------------------------------------- x = input('Enter x '); if x >= 0 y = 1; else y = -1; end y 》 test6 Enter x 8 y = 1 》 test6 Enter x -9 y = -1 》 ------------------------------------------- a = input('Enter a '); b = input('Enter b '); if b ~= 0 y = a/b end 》 test5 Enter a 10 Enter b -5 y = -2 》 test ??? 'test' は未定義の関数、または変数です. 》 test5 Enter a 100 Enter b 0 》 test5 Enter a 222 Enter b 11 y = 20.1818 》
返回