matlab中ode45函数编写

matlab中ode45函数编写
matlab中ode45函数编写

function varargout = ode45(ode,tspan,y0,options,varargin)

%ODE45 Solve non-stiff differential equations, medium order method.

% [TOUT,YOUT] = ODE45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates % the system of differential equations y' = f(t,y) from time T0 to TFINAL % with initial conditions Y0. ODEFUN is a function handle. For a scalar T

% and a vector Y, ODEFUN(T,Y) must return a column vector corresponding % to f(t,y). Each row in the solution array YOUT corresponds to a time % returned in the column vector TOUT. To obtain solutions at specific % times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = % [T0 T1 ... TFINAL].

%

% [TOUT,YOUT] = ODE45(ODEFUN,TSPAN,Y0,OPTIONS) solves as above with default

% integration properties replaced by values in OPTIONS, an argument created % with the ODESET function. See ODESET for details. Commonly used options % are scalar relative error tolerance 'RelTol' (1e-3 by default) and vector % of absolute error tolerances 'AbsTol' (all components 1e-6 by default). % If certain components of the solution must be non-negative, use

% ODESET to set the 'NonNegative' property to the indices of these

% components.

%

% ODE45 can solve problems M(t,y)*y' = f(t,y) with mass matrix M that is % nonsingular. Use ODESET to set the 'Mass' property to a function handle % MASS if MASS(T,Y) returns the value of the mass matrix. If the mass matrix % is constant, the matrix can be used as the value of the 'Mass' option. If

% the mass matrix does not depend on the state variable Y and the function % MASS is to be called with one input argument T, set 'MStateDependence' to

% 'none'. ODE15S and ODE23T can solve problems with singular mass matrices. %

% [TOUT,YOUT,TE,YE,IE] = ODE45(ODEFUN,TSPAN,Y0,OPTIONS) with the 'Events' % property in OPTIONS set to a function handle EVENTS, solves as above % while also finding where functions of (T,Y), called event functions, % are zero. For each function you specify whether the integration is

% to terminate at a zero and whether the direction of the zero crossing % matters. These are the three column vectors returned by EVENTS:

% [VALUE,ISTERMINAL,DIRECTION] = EVENTS(T,Y). For the I-th event function: % VALUE(I) is the value of the function, ISTERMINAL(I)=1 if the integration % is to terminate at a zero of this event function and 0 otherwise.

% DIRECTION(I)=0 if all zeros are to be computed (the default), +1 if only % zeros where the event function is increasing, and -1 if only zeros where % the event function is decreasing. Output TE is a column vector of times % at which events occur. Rows of YE are the corresponding solutions, and % indices in vector IE specify which event occurred.

%

% SOL = ODE45(ODEFUN,[T0 TFINAL],Y0...) returns a structure that can be % used with DEVAL to evaluate the solution or its first derivative at

% any point between T0 and TFINAL. The steps chosen by ODE45 are returned % in a row vector SOL.x. For each I, the column SOL.y(:,I) contains

% the solution at SOL.x(I). If events were detected, SOL.xe is a row vector % of points at which events occurred. Columns of SOL.ye are the corresponding

% solutions, and indices in vector SOL.ie specify which event occurred. %

% Example

% [t,y]=ode45(@vdp1,[0 20],[2 0]);

% plot(t,y(:,1));

% solves the system y' = vdp1(t,y), using the default relative error % tolerance 1e-3 and the default absolute tolerance of 1e-6 for each % component, and plots the first component of the solution.

%

% Class support for inputs TSPAN, Y0, and the result of ODEFUN(T,Y):

% float: double, single

%

% See also

% other ODE solvers: ODE23, ODE113, ODE15S, ODE23S, ODE23T, ODE23TB % implicit ODEs: ODE15I

% options handling: ODESET, ODEGET

% output functions: ODEPLOT, ODEPHAS2, ODEPHAS3, ODEPRINT

% evaluating solution: DEVAL

% ODE examples: RIGIDODE, BALLODE, ORBITODE

% function handles: FUNCTION_HANDLE

%

% NOTE:

% The interpretation of the first input argument of the ODE solvers and % some properties available through ODESET have changed in MATLAB 6.0. % Although we still support the v5 syntax, any new functionality is % available only with the new syntax. To see the v5 help, type in

% the command line

% more on, type ode45, more off

% NOTE:

% This portion describes the v5 syntax of ODE45.

%

% [T,Y] = ODE45('F',TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the

% system of differential equations y' = F(t,y) from time T0 to TFINAL with % initial conditions Y0. 'F' is a string containing the name of an ODE % file. Function F(T,Y) must return a column vector. Each row in

% solution array Y corresponds to a time returned in column vector T. To % obtain solutions at specific times T0, T1, ..., TFINAL (all increasing % or all decreasing), use TSPAN = [T0 T1 ... TFINAL].

%

% [T,Y] = ODE45('F',TSPAN,Y0,OPTIONS) solves as above with default

% integration parameters replaced by values in OPTIONS, an argument

% created with the ODESET function. See ODESET for details. Commonly

% used options are scalar relative error tolerance 'RelTol' (1e-3 by

% default) and vector of absolute error tolerances 'AbsTol' (all

% components 1e-6 by default).

%

% [T,Y] = ODE45('F',TSPAN,Y0,OPTIONS,P1,P2,...) passes the additional

% parameters P1,P2,... to the ODE file as F(T,Y,FLAG,P1,P2,...) (see

% ODEFILE). Use OPTIONS = [] as a place holder if no options are set. %

% It is possible to specify TSPAN, Y0 and OPTIONS in the ODE file (see % ODEFILE). If TSPAN or Y0 is empty, then ODE45 calls the ODE file

% [TSPAN,Y0,OPTIONS] = F([],[],'init') to obtain any values not supplied % in the ODE45 argument list. Empty arguments at the end of the call list % may be omitted, e.g. ODE45('F').

%

% ODE45 can solve problems M(t,y)*y' = F(t,y) with a mass matrix M that is

% nonsingular. Use ODESET to set Mass to 'M', 'M(t)', or 'M(t,y)' if the

% ODE file is coded so that F(T,Y,'mass') returns a constant,

% time-dependent, or time- and state-dependent mass matrix, respectively. % The default value of Mass is 'none'. ODE15S and ODE23T can solve problems % with singular mass matrices.

%

% [T,Y,TE,YE,IE] = ODE45('F',TSPAN,Y0,OPTIONS) with the Events property in

% OPTIONS set to 'on', solves as above while also locating zero crossings % of an event function defined in the ODE file. The ODE file must be

% coded so that F(T,Y,'events') returns appropriate information. See

% ODEFILE for details. Output TE is a column vector of times at which % events occur, rows of YE are the corresponding solutions, and indices in

% vector IE specify which event occurred.

%

% See also ODEFILE

% ODE45 is an implementation of the explicit Runge-Kutta (4,5) pair of % Dormand and Prince called variously RK5(4)7FM, DOPRI5, DP(4,5) and DP54. % It uses a "free" interpolant of order 4 communicated privately by

% Dormand and Prince. Local extrapolation is done.

% Details are to be found in The MATLAB ODE Suite, L. F. Shampine and

% M. W. Reichelt, SIAM Journal on Scientific Computing, 18-1, 1997.

% Mark W. Reichelt and Lawrence F. Shampine, 6-14-94

% Copyright 1984-2009 The MathWorks, Inc.

% $Revision: 5.74.4.10 $ $Date: 2009/04/21 03:24:15 $

solver_name = 'ode45';

% Check inputs

if nargin < 4

options = [];

if nargin < 3

y0 = [];

if nargin < 2

tspan = [];

if nargin < 1

error('MATLAB:ode45:NotEnoughInputs',...

'Not enough input arguments. See ODE45.');

end

end

end

end

% Stats

nsteps = 0;

nfailed = 0;

nfevals = 0;

% Output

FcnHandlesUsed = isa(ode,'function_handle');

output_sol = (FcnHandlesUsed && (nargout==1)); % sol = odeXX(...) output_ty = (~output_sol && (nargout > 0)); % [t,y,...] = odeXX(...)

% There might be no output requested...

sol = []; f3d = [];

if output_sol

sol.solver = solver_name;

sol.extdata.odefun = ode;

sol.extdata.options = options;

sol.extdata.varargin = varargin;

end

% Handle solver arguments

[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... options, threshold, rtol, normcontrol, normy, hmax, htry, htspan, dataType] = ...

odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

nfevals = nfevals + 1;

% Handle the output

if nargout > 0

outputFcn = odeget(options,'OutputFcn',[],'fast');

else

outputFcn = odeget(options,'OutputFcn',@odeplot,'fast');

end

outputArgs = {};

if isempty(outputFcn)

haveOutputFcn = false;

else

haveOutputFcn = true;

outputs = odeget(options,'OutputSel',1:neq,'fast');

if isa(outputFcn,'function_handle')

% With MATLAB 6 syntax pass additional input arguments to outputFcn. outputArgs = varargin;

end

end

refine = max(1,odeget(options,'Refine',4,'fast'));

if ntspan > 2

outputAt = 'RequestedPoints'; % output only at tspan points

elseif refine <= 1

outputAt = 'SolverSteps'; % computed points, no refinement else

outputAt = 'RefinedSteps'; % computed points, with refinement S = (1:refine-1) / refine;

end

printstats = strcmp(odeget(options,'Stats','off','fast'),'on');

% Handle the event function

[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...

odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);

% Handle the mass matrix

[Mtype, M, Mfun] = odemass(FcnHandlesUsed,odeFcn,t0,y0,options,varargin); if Mtype > 0 % non-trivial mass matrix

Msingular = odeget(options,'MassSingular','no','fast');

if strcmp(Msingular,'maybe')

warning('MATLAB:ode45:MassSingularAssumedNo',['ODE45 assumes '...

'MassSingular is ''no''. See ODE15S or ODE23T.']);

elseif strcmp(Msingular,'yes')

error('MATLAB:ode45:MassSingularYes',...

['MassSingular cannot be ''yes'' for this solver. See ODE15S '...

' or ODE23T.']);

end

% Incorporate the mass matrix into odeFcn and odeArgs.

[odeFcn,odeArgs] =

odemassexplicit(FcnHandlesUsed,Mtype,odeFcn,odeArgs,Mfun,M);

f0 = feval(odeFcn,t0,y0,odeArgs{:});

nfevals = nfevals + 1;

end

% Non-negative solution components

idxNonNegative = odeget(options,'NonNegative',[],'fast');

nonNegative = ~isempty(idxNonNegative);

if nonNegative % modify the derivative function

[odeFcn,thresholdNonNegative] =

odenonnegative(odeFcn,y0,threshold,idxNonNegative);

f0 = feval(odeFcn,t0,y0,odeArgs{:});

nfevals = nfevals + 1;

end

t = t0;

y = y0;

% Allocate memory if we're generating output.

nout = 0;

tout = []; yout = [];

if nargout > 0

if output_sol

chunk = min(max(100,50*refine), refine+floor((2^11)/neq));

tout = zeros(1,chunk,dataType);

yout = zeros(neq,chunk,dataType);

f3d = zeros(neq,7,chunk,dataType);

else

if ntspan > 2 % output only at tspan points

tout = zeros(1,ntspan,dataType);

yout = zeros(neq,ntspan,dataType);

else% alloc in chunks

chunk = min(max(100,50*refine), refine+floor((2^13)/neq));

tout = zeros(1,chunk,dataType);

yout = zeros(neq,chunk,dataType);

end

end

nout = 1;

tout(nout) = t;

yout(:,nout) = y;

end

% Initialize method parameters.

pow = 1/5;

A = [1/5, 3/10, 4/5, 8/9, 1, 1];

B = [

1/5 3/40 44/45 19372/6561 9017/3168 35/384

0 9/40 -56/15 -25360/2187 -355/33 0

0 0 32/9 64448/6561 46732/5247 500/1113

0 0 0 -212/729 49/176 125/192

0 0 0 0 -5103/18656 -2187/6784

0 0 0 0 0 11/84

0 0 0 0 0 0

];

E = [71/57600; 0; -71/16695; 71/1920; -17253/339200; 22/525; -1/40];

f = zeros(neq,7,dataType);

hmin = 16*eps(t);

if isempty(htry)

% Compute an initial step size h using y'(t).

absh = min(hmax, htspan);

if normcontrol

rh = (norm(f0) / max(normy,threshold)) / (0.8 * rtol^pow);

else

rh = norm(f0 ./ max(abs(y),threshold),inf) / (0.8 * rtol^pow);

end

if absh * rh > 1

absh = 1 / rh;

end

absh = max(absh, hmin);

else

absh = min(hmax, max(hmin, htry));

end

f(:,1) = f0;

% Initialize the output function.

if haveOutputFcn

feval(outputFcn,[t tfinal],y(outputs),'init',outputArgs{:});

end

% THE MAIN LOOP

done = false;

while ~done

% By default, hmin is a small number such that t+hmin is only slightly % different than t. It might be 0 if t is 0.

hmin = 16*eps(t);

absh = min(hmax, max(hmin, absh)); % couldn't limit absh until new hmin h = tdir * absh;

% Stretch the step if within 10% of tfinal-t.

if 1.1*absh >= abs(tfinal - t)

h = tfinal - t;

absh = abs(h);

done = true;

end

% LOOP FOR ADVANCING ONE STEP.

nofailed = true; % no failed attempts

while true

hA = h * A;

hB = h * B;

f(:,2) = feval(odeFcn,t+hA(1),y+f*hB(:,1),odeArgs{:});

f(:,3) = feval(odeFcn,t+hA(2),y+f*hB(:,2),odeArgs{:});

f(:,4) = feval(odeFcn,t+hA(3),y+f*hB(:,3),odeArgs{:});

f(:,5) = feval(odeFcn,t+hA(4),y+f*hB(:,4),odeArgs{:});

f(:,6) = feval(odeFcn,t+hA(5),y+f*hB(:,5),odeArgs{:});

tnew = t + hA(6);

if done

tnew = tfinal; % Hit end point exactly.

end

h = tnew - t; % Purify h.

ynew = y + f*hB(:,6);

f(:,7) = feval(odeFcn,tnew,ynew,odeArgs{:});

nfevals = nfevals + 6;

% Estimate the error.

NNrejectStep = false;

if normcontrol

normynew = norm(ynew);

errwt = max(max(normy,normynew),threshold);

err = absh * (norm(f * E) / errwt);

if nonNegative && (err <= rtol) && any(ynew(idxNonNegative)<0)

errNN = norm( max(0,-ynew(idxNonNegative)) ) / errwt ;

if errNN > rtol

err = errNN;

NNrejectStep = true;

end

end

else

err = absh * norm((f * E) ./ max(max(abs(y),abs(ynew)),threshold),inf);

if nonNegative && (err <= rtol) && any(ynew(idxNonNegative)<0)

errNN = norm( max(0,-ynew(idxNonNegative)) ./ thresholdNonNegative, inf);

if errNN > rtol

err = errNN;

NNrejectStep = true;

end

end

end

% Accept the solution only if the weighted error is no more than the % tolerance rtol. Estimate an h that will yield an error of rtol on % the next step or the next try at taking this step, as the case may be, % and use 0.8 of this value to avoid failures.

if err > rtol % Failed step

nfailed = nfailed + 1;

if absh <= hmin

warning('MATLAB:ode45:IntegrationTolNotMet',['Failure at t=%e. '...

'Unable to meet integration tolerances without reducing '...

'the step size below the smallest value allowed (%e) '...

'at time t.'],t,hmin);

solver_output = odefinalize(solver_name, sol,...

outputFcn, outputArgs,...

printstats, [nsteps, nfailed, nfevals],... nout, tout, yout,...

haveEventFcn, teout, yeout, ieout,...

{f3d,idxNonNegative});

if nargout > 0

varargout = solver_output;

end

return;

end

if nofailed

nofailed = false;

if NNrejectStep

absh = max(hmin, 0.5*absh);

else

absh = max(hmin, absh * max(0.1, 0.8*(rtol/err)^pow));

end

else

absh = max(hmin, 0.5 * absh);

end

h = tdir * absh;

done = false;

else% Successful step

NNreset_f7 = false;

if nonNegative && any(ynew(idxNonNegative)<0)

ynew(idxNonNegative) = max(ynew(idxNonNegative),0);

if normcontrol

normynew = norm(ynew);

end

NNreset_f7 = true;

end

break;

end

end

nsteps = nsteps + 1;

if haveEventFcn

[te,ye,ie,valt,stop] = ...

odezero(@ntrp45,eventFcn,eventArgs,valt,t,y,tnew,ynew,t0,h,f,idxNonNega tive);

if ~isempty(te)

if output_sol || (nargout > 2)

teout = [teout, te];

yeout = [yeout, ye];

ieout = [ieout, ie];

end

if stop % Stop on a terminal event.

% Adjust the interpolation data to [t te(end)].

% Update the derivatives using the interpolating polynomial.

taux = t + (te(end) - t)*A;

[~,f(:,2:7)] = ntrp45(taux,t,y,[],[],h,f,idxNonNegative);

tnew = te(end);

ynew = ye(:,end);

h = tnew - t;

done = true;

end

end

end

if output_sol

nout = nout + 1;

if nout > length(tout)

tout = [tout, zeros(1,chunk,dataType)]; % requires chunk >= refine yout = [yout, zeros(neq,chunk,dataType)];

f3d = cat(3,f3d,zeros(neq,7,chunk,dataType));

end

tout(nout) = tnew;

yout(:,nout) = ynew;

f3d(:,:,nout) = f;

end

if output_ty || haveOutputFcn

switch outputAt

case'SolverSteps'% computed points, no refinement

nout_new = 1;

tout_new = tnew;

yout_new = ynew;

case'RefinedSteps'% computed points, with refinement

tref = t + (tnew-t)*S;

nout_new = refine;

tout_new = [tref, tnew];

yout_new = [ntrp45(tref,t,y,[],[],h,f,idxNonNegative), ynew];

case'RequestedPoints'% output only at tspan points

nout_new = 0;

tout_new = [];

yout_new = [];

while next <= ntspan

if tdir * (tnew - tspan(next)) < 0

if haveEventFcn && stop % output tstop,ystop

nout_new = nout_new + 1;

tout_new = [tout_new, tnew];

yout_new = [yout_new, ynew];

end

break;

end

nout_new = nout_new + 1;

tout_new = [tout_new, tspan(next)];

if tspan(next) == tnew

yout_new = [yout_new, ynew];

else

yout_new = [yout_new,

ntrp45(tspan(next),t,y,[],[],h,f,idxNonNegative)];

end

next = next + 1;

end

end

if nout_new > 0

if output_ty

oldnout = nout;

nout = nout + nout_new;

if nout > length(tout)

tout = [tout, zeros(1,chunk,dataType)]; % requires chunk >= refine yout = [yout, zeros(neq,chunk,dataType)];

end

idx = oldnout+1:nout;

tout(idx) = tout_new;

yout(:,idx) = yout_new;

end

if haveOutputFcn

stop =

feval(outputFcn,tout_new,yout_new(outputs,:),'',outputArgs{:});

if stop

done = true;

end

end

end

end

if done

break

end

% If there were no failures compute a new h.

if nofailed

% Note that absh may shrink by 0.8, and that err may be 0.

temp = 1.25*(err/rtol)^pow;

if temp > 0.2

absh = absh / temp;

else

absh = 5.0*absh;

end

end

% Advance the integration one step.

t = tnew;

y = ynew;

if normcontrol

normy = normynew;

end

if NNreset_f7

% Used f7 for unperturbed solution to interpolate.

% Now reset f7 to move along constraint.

f(:,7) = feval(odeFcn,tnew,ynew,odeArgs{:});

nfevals = nfevals + 1;

end

f(:,1) = f(:,7); % Already have f(tnew,ynew)

end

solver_output = odefinalize(solver_name, sol,...

outputFcn, outputArgs,...

printstats, [nsteps, nfailed, nfevals],... nout, tout, yout,...

haveEventFcn, teout, yeout, ieout,...

{f3d,idxNonNegative});

if nargout > 0

varargout = solver_output;

end

(完整版)MATLAB常用函数大全

一、MATLAB常用的基本数学函数 abs(x):纯量的绝对值或向量的长度 angle(z):复数z的相角(Phase angle) sqrt(x):开平方 real(z):复数z的实部 imag(z):复数z的虚部 conj(z):复数z的共轭复数 round(x):四舍五入至最近整数 fix(x):无论正负,舍去小数至最近整数 floor(x):地板函数,即舍去正小数至最近整数ceil(x):天花板函数,即加入正小数至最近整数rat(x):将实数x化为分数表示 rats(x):将实数x化为多项分数展开 sign(x):符号函数(Signum function)。 当x<0时,sign(x)=-1; 当x=0时,sign(x)=0; 当x>0时,sign(x)=1。 rem(x,y):求x除以y的馀数 gcd(x,y):整数x和y的最大公因数 lcm(x,y):整数x和y的最小公倍数 exp(x):自然指数 pow2(x):2的指数 log(x):以e为底的对数,即自然对数或 log2(x):以2为底的对数 log10(x):以10为底的对数 二、MATLAB常用的三角函数 sin(x):正弦函数 cos(x):余弦函数

tan(x):正切函数 asin(x):反正弦函数 acos(x):反馀弦函数 atan(x):反正切函数 atan2(x,y):四象限的反正切函数 sinh(x):超越正弦函数 cosh(x):超越馀弦函数 tanh(x):超越正切函数 asinh(x):反超越正弦函数 acosh(x):反超越馀弦函数 atanh(x):反超越正切函数 三、适用於向量的常用函数有: min(x): 向量x的元素的最小值 max(x): 向量x的元素的最大值 mean(x): 向量x的元素的平均值 median(x): 向量x的元素的中位数 std(x): 向量x的元素的标准差 diff(x): 向量x的相邻元素的差 sort(x): 对向量x的元素进行排序(Sorting)length(x): 向量x的元素个数 norm(x): 向量x的欧氏(Euclidean)长度sum(x): 向量x的元素总和 prod(x): 向量x的元素总乘积 cumsum(x): 向量x的累计元素总和cumprod(x): 向量x的累计元素总乘积 dot(x, y): 向量x和y的内积 cross(x, y): 向量x和y的外积 四、MATLAB的永久常数

(完整版)matlab函数大全(非常实用)

信源函数 randerr 产生比特误差样本 randint 产生均匀分布的随机整数矩阵 randsrc 根据给定的数字表产生随机矩阵 wgn 产生高斯白噪声 信号分析函数 biterr 计算比特误差数和比特误差率 eyediagram 绘制眼图 scatterplot 绘制分布图 symerr 计算符号误差数和符号误差率 信源编码 compand mu律/A律压缩/扩张 dpcmdeco DPCM(差分脉冲编码调制)解码dpcmenco DPCM编码 dpcmopt 优化DPCM参数 lloyds Lloyd法则优化量化器参数 quantiz 给出量化后的级和输出值 误差控制编码 bchpoly 给出二进制BCH码的性能参数和产生多项式convenc 产生卷积码 cyclgen 产生循环码的奇偶校验阵和生成矩阵cyclpoly 产生循环码的生成多项式 decode 分组码解码器 encode 分组码编码器 gen2par 将奇偶校验阵和生成矩阵互相转换gfweight 计算线性分组码的最小距离 hammgen 产生汉明码的奇偶校验阵和生成矩阵rsdecof 对Reed-Solomon编码的ASCII文件解码rsencof 用Reed-Solomon码对ASCII文件编码rspoly 给出Reed-Solomon码的生成多项式syndtable 产生伴随解码表 vitdec 用Viterbi法则解卷积码 (误差控制编码的低级函数) bchdeco BCH解码器 bchenco BCH编码器 rsdeco Reed-Solomon解码器 rsdecode 用指数形式进行Reed-Solomon解码 rsenco Reed-Solomon编码器 rsencode 用指数形式进行Reed-Solomon编码 调制与解调

matlab所有函数集合

一、常用命令 1.常用命令-->管理命令和函数 addpath 添加目录到MA TLAB 搜索路径 doc 在Web 浏览器上现实HTML 文档 help 显示Matlab 命令和M 文件的在线帮助 helpwin helpdesk help lookfor 在基于Matlab 搜索路径的所有M 文件中搜索关键字partialpath 部分路径名 path 所有关于路径名的处理 pathtool 一个不错的窗口路径处理界面 rmpath 删除搜索路径中指定目录 type 显示指定文件的内容 ver 版本信息 version 版本号 web 打开web 页 what 列出当前目录吓所有的M 文件 Mat 文件和 Mex 文件whatsnew 显示readme 文件 which 显示文件位置 2.常用命令-->管理变量和工作区(输入输出、内存管理等) clear 从内存中删除 disp 显示文本或数组内容 length 数组长度(最长维数) load 重新载入变量(从磁盘上) mlock 锁定文件,防止文件被错误删除 munlock 解锁文件 openvar 在数组编辑器中打开变量 pack 整理内存空间 save 保存变量到文件 8*) size 数组维数 who 列出内存变量 whos 列出内存变量,同时显示变量维数 workspace 显示工作空间窗口 3.常用命令-->管理命令控制窗口(command 窗口) clc 清空命令窗口 echo 禁止或允许显示执行过程 format 设置输出显示格式 home 光标移动到命令窗口左上角 more 设置命令窗口页输出格式 4.常用命令-->文件和工作环境 cd 改变工作目录 copyfile 复制文件 delete 删除文件和图形对象 diary 把命令窗口的人机交互保存到文件

MATLAB_M文件与M函数

M文件与M函数 Matlab输入命令的常用方式有两种:一种是直接在Matlab的命令窗门中逐条输入Matlab 命令;二是m文件工作方式。当命令行很简单时,使用逐条输入方式还是比较方便的。但当命令行很多时(比如说几十行乃至全成百上千行命令),显然再使用这种方式输入MATLAB 命令,就会显得杂乱无章,不易于把握程序的具体走向,并且给程序的修改和维护带来了很大的麻烦。这时,建议采用Matlab命令的第二种输入形式m文件工作方式。 m文件工作方式,指的是将要执行的命令全部写在一个文本文件中,这样既能使程序显得简洁明了,又便于对程序的修改与维护。m文件直接采用Matlab命令编写,就像在Matlab 的命令窗口直接输入命令一样,因此调试起来也十分方便,并且增强了程序的交互性。 m文件与其他文本文件一样,可以在任何文本编辑器中进打编辑、存储、修改和读取。利用m文件还可以根据白己的需要编写一些函数,这些函数也可以橡Matlab提供的函数一样进行调用。从某种意义上说,这也是对MATLAB的二次开发。 m文件有两种形式:一种是命令方式或称脚本方式;另一种就是函数文件形式。两种形式的文件扩展名均是.m。 1、M文件 当遇到输入命令较多以及要重复输入命令的情况时,利用命令文件就显得很方便了。将所有要执行的命令按顺序放到一个扩展名为.m的文本文件中,每次运行时只需在MATLAB 的命令窗口输入m文件的文件名就可以了。需要注意的是,m文件最好直接放在Matlab 的默认搜索路径下(一般是Matlab安装目录的子目录work中),这样就不用设置m文件的路径了,否则应当用路径操作指令path重新设置路径。另外,m文件名不应该与Matlab的内置函数名以及工具箱中的函数重名,以免发生执行错误命令的现象。Matlab对命令文件的执行等价于从命令窗口中顺序执行文件中的所有指令。命令文件可以访问Matlab工作空间里的任何变量及数据。命令文件运行过程中产生的所有变量都等价于从Matlab工作空间中创建这些变量。因此,任何其他命令文件和函数都可以自由地访问这些变量。这些变量一旦产生就一直保存在内存中,只有对它们重新赋值,它们的原有值才会变化。关机后,这里变量也就全部消失了。另外,在命令窗口中运行clear命令,也可以把这些变量从工作空间中删去。当然,在Matlab的工作空间窗口中也可以用鼠标选择想要删除的变量,从而将这些变量从工作空间中删除。 接下来,编写一个名为test.m的命令文件,用来计算矩阵1到100的和,并把它放到变量s中。 第一步创建新的M-文件。在Matlab主菜单上选择菜单命令File→New→M-File

(仅供参考)Matlab编写与调用函数

MATLAB 学习指南 第六章.编写与调用函数 在这一章中,我们讨论如何用多源代码文件来构造一个程序。首先,解释代码文件在MATLAB中如何工作。在编译语言中,例如FORTRAN,C ,或C++,代码被存储在一个或多个源文件中,在进行编译的时候,这些源文件组合在一起 形成了一个单独的可执行文件。作为一种解释型语言,MATLAB以一种更广泛的方式来处理多个源文件。MATLAB代码被放入带有扩展名.m的ASCII文件(或称m-文件)中。MATLAB 6 有一个集成字处理与调试应用程序,尽管会用到其它编辑程序如vi或emacs,集成字处理与调试应用程序仍是编译m-文件的首选程序。 有两种不同的m-文件。一种是脚本文件,它是一种最简单的文件,仅仅将MATLAB中的指令收集在一起。当在交互提示符处输入文件名执行脚本文件时,MATLAB在m-文件内读取并执行指令,就好像指令是我们输入的。而且,似乎我们能够削减m-文件的内容并将削减过的内容传到MATLAB指令窗口中。这种m-文件的用法将在6.1节中给予概述。 在6.2节中要讨论的第二种m-文件包含一个单一函数,此函数名与此m-文件名相同。这种m-文件包含一段独立的代码,这段代码具有一个明确规定的输入/输出界面;那就是说,传给这段代码一列空变量arg1,arg2,…,这段独立代码就能够被调用,然后返回输出值out1,out2,…。一个函数m-文件的第一个非注释行包含函数标头,其形式如下: 此m-文件以返回指令结束,将执行程序返回到函数被调用的位置。或者在交互指令提示符处或者在另一个m-文件内,无论何时用下列指令调用函数代码,函数代码都将被执行。 输入映射到空变量:arg1=var1,arg2=var2,等等。在函数主体内,输出值被分配给了变量out1,out2,等等。当遇到返回值时,当前值out1,out2,…在函数被调用处被映射到变量outvar1,outvar2,…。在用可变长度自变量和输出变量列表编写函数时,MATLAB允许更多的自由。例如,也可以使用下列指令来调用函数。 在此情况下,仅返回一个单一输出变量,这个变量在出口处包含函数变量out1的值。输入和输出自变量可能是字符串,数值,向量,矩阵,或者更高级的数据结构。 为什么使用函数呢?因为从每门计算机科学课程中可知,把一个大的程序分割 成多个可以单独执行一个被明确规定的和被注释过的任务的小程序会使大程序 易读,易于修改,不易于出错。在MATLAB中,先为程序编写一个主文件,或者是一个脚本文件或者更好的话,是一个能够返回一个单一整数的函数m-文件(返回1表示程序执行成功,0表示不完全程序执行,负值表示出现运行误差),这个主文件是程序的进入点。通过把m-文件当作函数来调用,此程序文件可以

Matlab中的函数

abs 绝对值、模、字符的ASCII码值? acos 反余弦? acosh 反双曲余弦? acot 反余切? acoth 反双曲余切? acsc 反余割? acsch 反双曲余割? align 启动图形对象几何位置排列工具? all 所有元素非零为真? angle 相角? ans 表达式计算结果的缺省变量名? any 所有元素非全零为真? area 面域图? argnames 函数M文件宗量名? asec 反正割? asech 反双曲正割? asin 反正弦? asinh 反双曲正弦? assignin 向变量赋值? atan 反正切? atan2 四象限反正切? atanh 反双曲正切? autumn 红黄调秋色图阵? axes 创建轴对象的低层指令? axis 控制轴刻度和风格的高层指令? B b? bar 二维直方图? bar3 三维直方图? bar3h 三维水平直方图? barh 二维水平直方图? base2dec X进制转换为十进制? bin2dec 二进制转换为十进制? blanks 创建空格串? bone 蓝色调黑白色图阵? box 框状坐标轴?

break while 或for 环中断指令? brighten 亮度控制? C c? capture (3版以前)捕获当前图形? cart2pol 直角坐标变为极或柱坐标? cart2sph 直角坐标变为球坐标? cat 串接成高维数组? caxis 色标尺刻度? cd 指定当前目录? cdedit 启动用户菜单、控件回调函数设计工具? cdf2rdf 复数特征值对角阵转为实数块对角阵? ceil 向正无穷取整? cell 创建元胞数组? cell2struct 元胞数组转换为构架数组? celldisp 显示元胞数组内容? cellplot 元胞数组内部结构图示? char 把数值、符号、内联类转换为字符对象? chi2cdf 分布累计概率函数? chi2inv 分布逆累计概率函数? chi2pdf 分布概率密度函数? chi2rnd 分布随机数发生器? chol Cholesky分解? clabel 等位线标识? cla 清除当前轴? class 获知对象类别或创建对象? clc 清除指令窗? clear 清除内存变量和函数? clf 清除图对象? clock 时钟? colorcube 三浓淡多彩交叉色图矩阵? colordef 设置色彩缺省值? colormap 色图? colspace 列空间的基? close 关闭指定窗口? colperm 列排序置换向量?

MATLAB实验五 函数文件

MATLAB实验报告 学院:光电学院 班级:073-1 姓名:刘颖 学号:200713503117

实验五 函数文件 1.定义一个函数文件,求给定复数的指数、对数、正弦和余弦,并在命令文件中调用该函数文件。 程序设计: function [e ln s c]=num(x) e=exp(x) ln=log(x) s=sin(x) c=cos(x) end 运行结果: >> num(5i) e = 0.2837 - 0.9589i ln = 1.6094 + 1.5708i s = 0 +74.2032i c = 74.2099 ans = 0.2837 - 0.9589i 2.一物理系统可用下列方程组来表示: ??? ? ??? ???????= ?????? ??? ??? ???????????? ??----g g m m N N a a m m m m 2121212 111001cos 0 0sin 00cos 0 sin 0sin cos θ θθ θθθ 从键盘输入 m 1 、 m 2 和θ的值,求 N a a 121、、和 N 2 的值。其中g 取9.8,输入θ时以角度为单位。 程序设计: 函数文件in.m: function [a1,a2,N1,N2]=in(m1,m2,t) g=9.8; A=[m1*cos(t) -m1 -sin(t) 0;m1*sin(t) 0 cos(t) 0;0 m2 -sin(t) 0;0 0 -cos(t) 1]; C=[0;m1*g;0;m2*g]; B=inv(A)*C; a1=B(1); a2=B(2); N1=B(3); N2=B(4); end 调用in.m 的命令文件: >> m1=1;m2=2;t=30*pi/180; >> [a1,a2,N1,N2]=in(m1,m2,t) 运行结果: a1 = 6.5333 a2 = 1.8860 N1 = 7.5440 N2 = 26.1333 4.设 f(x)= 01 .01 1 .01 ) 3() 2(4 2 +++--x x , 编写一个MATLAB 函数文件fx.m ,使得调用f(x)时,x 可用矩阵代入,得出的f(x)为同阶矩阵。 程序设计: 函数文件fx.m: function A=fx(x) A=1./((x-2).^2+0.1)+1./(((x-3).^4)+0.01) end 调用fx.m 的命令文件: >> A=fx([1 2;2 3;4 3]) 运行结果: A = 0.9716 10.9901 10.9901 100.9091 1.2340 100.9091 5.已知y= ) 20()30() 40(f f f + (1)当f(n)=n+10ln(n 2+5)时,求y 的值。

matlab中s函数编写心得(转)(最新整理)

matlab中s函数编写心得(转) Part I: 所谓s函数是system Function的简称, 用它来写自己的simulink模块. s函数可以用matlab、C、C++、Fortran、Ada等语言来写,这儿我只介绍怎样用matlab语言来写吧(主要是它比较简单)< xmlnamespace prefix ="o" ns ="urn:schemas- microsoft-com:office:office" /> 先讲讲为什么要用s函数,我觉得用s函数可以利用matlab的丰富资源,而不仅仅局限于simulink提供的模块,而用c或c++等语言写的s函数还可以实现对硬件端口的操作,还可以操作windows API等 先介绍一下simulink的仿真过程(以便理解s函数),simulink 的仿真有两个阶段:一个为初始化,这个阶段主要是设置一些参数,像系统的输入输出个数、状态初值、采样时间等;第二个阶段就是运行阶段,这个阶段里要进行计算输出、更新离散状态、计算连续状态等等,这个阶段需要反复运行,直至结束. 在matlab的workspace里输入edit sfuntmpl(这是matlab自己提供的s函数模板),我们看它来具体分析s函数的结构.

1. 函数的函数头 函数的第一行:function [sys,x0,str,ts]=sfuntmpl(t,x,u,flag) , 先讲输入与输出变量的含义: t是采样时间, x是状态变量, u是输入(是做成simulink模块的输入) , flag是仿真过程中的状态标志(以它来判断当前是初始化还是运行等) sys输出根据flag的不同而不同(下面将结合flag来讲sys的含义) , x0是状态变量的初始值, str是保留参数(mathworks公司还没想好该怎么用它, 一般在初始化中将它置空就可以了, str=[]), ts是一个1×2的向量, ts(1)是采样周期, ts(2)是偏移量 2. 函数分析 下面结合sfuntmpl.m中的代码来讲具体的结构: switch flag, %判断flag,看当前处于哪个状态 case 0, [sys,x0,str,ts]=mdlInitializeSizes; // 解释说明 flag=0表示当前处于初始化状态,此时调用函数mdlInitializeSizes进行初始化,此函数在该文件的第149行定义. 其中的参数sys是一个结构体,它用来设置模块的一些参数,各个参 数详细说明如下 size = simsizes;%用于设置模块参数的结构体用simsizes来生

MATLAB函数大全(MATLAB函数总集,史上最全)

MATLAB函数大全 代充全国移动、联通、电信话费、腾讯QQ业务、网游点卡 淘宝店址:https://www.360docs.net/doc/e07846282.html,/ 信誉至上,服务第一 A a abs 绝对值、模、字符的ASCII码值 acos 反余弦 acosh 反双曲余弦 acot 反余切 acoth 反双曲余切 acsc 反余割 acsch 反双曲余割 align 启动图形对象几何位置排列工具 all 所有元素非零为真 angle 相角 ans 表达式计算结果的缺省变量名 any 所有元素非全零为真 area 面域图 argnames 函数M文件宗量名 asec 反正割 asech 反双曲正割 asin 反正弦 asinh 反双曲正弦 assignin 向变量赋值 atan 反正切 atan2 四象限反正切 atanh 反双曲正切 autumn 红黄调秋色图阵 axes 创建轴对象的低层指令 axis 控制轴刻度和风格的高层指令 B b bar 二维直方图 bar3 三维直方图 bar3h 三维水平直方图 barh 二维水平直方图 base2dec X进制转换为十进制

bin2dec 二进制转换为十进制 blanks 创建空格串 代充全国移动、联通、电信话费、腾讯QQ业务、网游点卡 淘宝店址:https://www.360docs.net/doc/e07846282.html,/ 信誉至上,服务第一 bone 蓝色调黑白色图阵 box 框状坐标轴 break while 或for 环中断指令 brighten 亮度控制 C c capture (3版以前)捕获当前图形 cart2pol 直角坐标变为极或柱坐标 cart2sph 直角坐标变为球坐标 cat 串接成高维数组 caxis 色标尺刻度 cd 指定当前目录 cdedit 启动用户菜单、控件回调函数设计工具 cdf2rdf 复数特征值对角阵转为实数块对角阵 ceil 向正无穷取整 cell 创建元胞数组 cell2struct 元胞数组转换为构架数组 celldisp 显示元胞数组内容 cellplot 元胞数组内部结构图示 char 把数值、符号、内联类转换为字符对象 chi2cdf 分布累计概率函数 chi2inv 分布逆累计概率函数 chi2pdf 分布概率密度函数 chi2rnd 分布随机数发生器 chol Cholesky分解 clabel 等位线标识 cla 清除当前轴 class 获知对象类别或创建对象 clc 清除指令窗 clear 清除内存变量和函数 clf 清除图对象 clock 时钟 colorcube 三浓淡多彩交叉色图矩阵

matlab程序设计实例

MATLAB 程序设计方法及若干程序实例 樊双喜 (河南大学数学与 信息科学学院开封475004) 摘要本文通过对 MATLAB 程序设计中的若干典型问题做简要的分析和总结,并在此基础上着重讨论了有关算法设计、程序的调试与测试、算法与程序的优化以及循环控制等方面的问题.还通过对一些程序实例做具体解析,来方便读者进行编程训练并掌握一些有关MATLAB 程序设计方面的基本概念、基本方法以及某些问题的处理技巧等.此外,在文章的最后还给出了几个常用数学方法的算法程序, 供读者参考使用.希望能对初学者进行 MATLAB 编程训练提供一些可供参考的材料,并起到一定的指导和激励作用,进而为MATLAB 编程入门打下好的基础. 关键字算法设计;程序调试与测试;程序优化;循环控制 1 算法与程序 1.1 算法与程序的关系算法被称为程序的灵魂,因此在介绍程序之前应先了 解什么是算法.所谓算 法就是对特定问题求解步骤的一种描述.对于一个较复杂的计算或是数据处理的问题,通常是先设计出在理论上可行的算法,即程序的操作步骤,然后再按照算法逐步翻译成相应的程序语言,即计算机可识别的语言. 所谓程序设计,就是使用在计算机上可执行的程序代码来有效的描述用于解决特定问题算法的过程.简单来说,程序就是指令的集合.结构化程序设计由于采用了模块分化与功能分解,自顶向下,即分而治之的方法,因而可将一个较复杂的问题分解为若干子问题,逐步求精.算法是操作的过程,而程序结构和程序流程则是算法的具体体现. 1.2MATLAB 语言的特点 MATLAB 语言简洁紧凑,使用方便灵活,库函数极其丰富,其语法规则与科技人员的思维和书写习惯相近,便于操作.MATLAB 程序书写形式自由,利用其丰富

(完整版)matlab函数大全最完整版

MATLAB函数大全 Matlab有没有求矩阵行数/列数/维数的函数? ndims(A)返回A的维数 size(A)返回A各个维的最大元素个数 length(A)返回max(size(A)) [m,n]=size(A)如果A是二维数组,返回行数和列数nnz(A)返回A中非0元素的个数 MATLAB的取整函数:fix(x), floor(x) :,ceil(x) , round(x) (1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans = 3 -3 (2)floor(x):不超过x 的最大整数.(高斯取整) >> floor( [3.12 -3.12]) ans =

3 -4 (3)ceil(x) : 大于x 的最小整数>> ceil( [3.12 -3.12]) ans = 4 -3 (4)四舍五入取整 >> round(3.12 -3.12) ans = >> round([3.12 -3.12]) ans =

3 -3 >> 如何用matlab生成随机数函数 rand(1) rand(n):生成0到1之间的n阶随机数方阵rand(m,n):生成0到1之间的m×n的随机数矩阵(现成的函数) 另外: Matlab随机数生成函数 betarnd 贝塔分布的随机数生成器 binornd 二项分布的随机数生成器 chi2rnd 卡方分布的随机数生成器 exprnd 指数分布的随机数生成器 frnd f分布的随机数生成器 gamrnd 伽玛分布的随机数生成器 geornd 几何分布的随机数生成器 hygernd 超几何分布的随机数生成器

最小二乘法Matlab自编函数实现及示例.docx

、最小二乘拟合原理 x= xl x2 ... xn y= yl y2 ... yn 求m 次拟合 ?力* y 卅…I ZA ; A T A = ZX 茁 X x i - X x i +1 ,- ? ? ? [函Oi …备F =⑷矿丄? A T y 所以m 次拟合曲线为y = a 0 +勿?怎+吐■审+???? +如■牙皿 二、 Matlab 实现程序 function p=funLSM (x, y, m) %x z y 为序列长度相等的数据向量,m 为拟合多项式次数 format short; A=zeros(m+l,m+l); for i=0:m for j=0:m A(i + 1, j + 1)=sum(x.A (i+j)); end b(i+1)=sum(x.A i.*y); end a=A\b 1; p=fliplr (a'); 三、 作业 题1:给出如下数据,使用最小二乘法球一次和二次拟合多项式(取小数点后3位) X 1.36 1.49 1.73 1.81 1.95 2.16 2.28 2.48 Y 14.094 15.069 16.844 17.378 18.435 19.949 20.963 22.495 解:

? x=[1.36 1.49 1.73 1. 81 1. 95 2. 16 2. 28 2. 48]: ? y=[14.094 15.069 16.844 17. 378 18.435 19.949 20.963 22.495]; >> p=funLSM(x, y? 1) P = 7.4639 3.9161 >> p=funLSM(x, y? 2) P = 0.3004 6.3145 4.9763 一次拟合曲线为: y = 7.464x+ 3.91S 二次拟合曲线为: y = +6.315^4-4.976 一次拟合仿真图

MATLAB中常用的函数

[转]MATLAB 主要函数(一) (2008-05-11 17:09:43) 转载 标签: 分类:IT matlab 函数 杂谈 MATLAB主要函数指令表(按功能分类)原贴地址:https://www.360docs.net/doc/e07846282.html,/casularm/archive/2007/04/20/1572638.aspx 1常用指令(General Purpose Commands) 1.1通用信息查询(General information) demo 演示程序 help 在线帮助指令 helpbrowser 超文本文档帮助信息 helpdesk 超文本文档帮助信息 helpwin 打开在线帮助窗 info MATLAB 和MathWorks 公司的信息 subscribe MATLAB 用户注册 ver MATLAB 和TOOLBOX 的版本信息 version MATLAB 版本 whatsnew 显示版本新特征 1.2工作空间管理(Managing the workspace) clear 从内存中清除变量和函数 exit 关闭MATLAB load 从磁盘中调入数据变量 pack 合并工作内存中的碎块 quit 退出MATLAB save 把内存变量存入磁盘 who 列出工作内存中的变量名

whos 列出工作内存中的变量细节 workspace 工作内存浏览器 1.3管理指令和函数(Managing commands and functions) edit 矩阵编辑器 edit 打开M 文件 inmem 查看内存中的P 码文件 mex 创建MEX 文件 open 打开文件 pcode 生成P 码文件 type 显示文件内容 what 列出当前目录上的M、MAT、MEX 文件 which 确定指定函数和文件的位置 1.4搜索路径的管理(Managing the seach patli) addpath 添加搜索路径 rmpath 从搜索路径中删除目录 path 控制MATLAB 的搜索路径 pathtool 修改搜索路径 1.5指令窗控制(Controlling the command window) beep 产生beep 声 echo 显示命令文件指令的切换开关 diary 储存MATLAB 指令窗操作内容 format 设置数据输出格式 more 命令窗口分页输出的控制开关 1.6操作系统指令(Operating system commands) cd 改变当前工作目录 computer 计算机类型 copyfile 文件拷贝 delete 删除文件 dir 列出的文件 dos 执行dos 指令并返还结果

matlab function非常全的 matlab 函数

一、常用对象操作:除了一般windows窗口的常用功能键外。 1、!dir 可以查看当前工作目录的文件。!dir&可以在dos状态下查看。 2、who 可以查看当前工作空间变量名,whos 可以查看变量名细节。 3、功能键: 功能键快捷键说明 方向上键Ctrl+P 返回前一行输入 方向下键Ctrl+N 返回下一行输入 方向左键Ctrl+B 光标向后移一个字符 方向右键Ctrl+F 光标向前移一个字符 Ctrl+方向右键Ctrl+R 光标向右移一个字符 Ctrl+方向左键Ctrl+L 光标向左移一个字符 home Ctrl+A 光标移到行首 End Ctrl+E 光标移到行尾 Esc Ctrl+U 清除一行 Del Ctrl+D 清除光标所在的字符 Backspace Ctrl+H 删除光标前一个字符 Ctrl+K 删除到行尾 Ctrl+C 中断正在执行的命令 4、clc可以命令窗口显示的内容,但并不清除工作空间。 二、函数及运算 1、运算符: +:加,-:减,*:乘,/:除,\:左除^:幂,‘:复数的共轭转置,():制定运算顺序。 2、常用函数表: sin( ) 正弦(变量为弧度) Cot( ) 余切(变量为弧度) sind( ) 正弦(变量为度数) Cotd( ) 余切(变量为度数) asin( ) 反正弦(返回弧度) acot( ) 反余切(返回弧度) Asind( ) 反正弦(返回度数) acotd( ) 反余切(返回度数) cos( ) 余弦(变量为弧度) exp( ) 指数 cosd( ) 余弦(变量为度数) log( ) 对数 acos( ) 余正弦(返回弧度) log10( ) 以10为底对数 acosd( ) 余正弦(返回度数) sqrt( ) 开方 tan( ) 正切(变量为弧度) realsqrt( ) 返回非负根 tand( ) 正切(变量为度数) abs( ) 取绝对值

MATLAB一些函数实例

1.三角波产生器 t=-3:0.01:3; f1=tripuls(t); subplot(3,1,1); plot(t,f1); axis([-3,3,-0.2,1.2]) set(gcf,'color','w'); f2=tripuls(t,4); subplot(3,1,2); plot(t,f2); axis([-3,3,-0.2,1.2]) %set(gcf,'color','w'); f3=tripuls(t,4,-1); subplot(3,1,3); plot(t,f3); axis([-3,3,-0.2,1.2]) 2.离散序列的相加与相乘 function[x,n]=jxl(x1,x2,n1,n2) n=min(min(n1),min(n2)):max(max(n1),max(n2)); s1=zeros(1,length(n));s2=s1; s1(find((n>=min(n1))&(n<=max(n1))==1))=x1; s2(find((n>=min(n2))&(n<=max(n2))==1))=x2; x=s1+s2;//x=s1.*s2:%序列乘 axis([(min(min(n1),min(n2))-1),(max(max(n1),max(n2))+1),(min(x)-0.5), (max(x)+0.5)]) 3.序列的反摺 function[x,n]=xlfz(x1,n1) x=fliplr(x1);n=fliplr(n1); stem(n,x,'filled') axis([min(n)-1,max(n)+1,min(x)-0.5,max(x)+0.5]) 4.序列的卷积 function[x,n]=gghconv(x1,x2,n1,n2) x=conv(x1,x2) ns=n1(1)+n2(1); leg=length(x1)+length(x2)-2; n=ns:(ns+leg) subplot(2,2,1) stem(n1,x1,'filled') title('x1(n)') xlabel('n') subplot(2,2,2)

matlab函数计算的一些简单例子1

MATLAB作业一1、试求出如下极限。 (1) 23 25 (2)(3) lim (5) x x x x x x x ++ + →∞ ++ + ,(2) 23 3 1 2 lim () x y x y xy x y →- → + + ,(3) 22 22 22 1cos() lim ()x y x y x y x y e+ → → -+ + 解:(1)syms x; f=((x+2)^(x+2))*((x+3)^(x+3))/((x+5)^(2*x+5)) limit(f,x,inf) =exp(-5) (2)syms x y; f=(x^2*y+x*y^3)/(x+y)^3; limit(limit(f,x,-1),y,2) =-6; (3)syms x y; f=(1-cos(x^2+y^2))/(x^2+y^2)*exp(x^2+y^2); limit(limit(f,x,0),y,0) =0 2、试求出下面函数的导数。 (1 )() y x=, (2)22 atan ln() y x y x =+ 解; (1)syms x; f=sqrt(x*sin(x)*sqrt(1-exp(x))); g= diff(f,x); g== (sin(x)*(1 - exp(x))^(1/2) + x*cos(x)*(1 - exp(x))^(1/2) - (x*exp(x)*sin(x))/(2*(1 - exp(x))^(1/2)))/(2*(x*sin(x)*(1 - exp(x))^(1/2))^(1/2)) pretty(g)= (2)syms x y; f=atan(y/x)-log(x^2+y^2) pretty(-simple(diff(f,x)/diff(f,y)))= 2 x + y =------- x - 2 y (3) 假设1 cos u- =,试验证 22 u u x y y x ?? = ???? 。 解:syms x y; u=1/cos(sqrt(x/y)); diff(diff(u,x),y)-diff(diff(u,y),x)=0; 所以: 22 u u x y y x ?? = ????

matlab中所有函数解析_太全了

A a abs 绝对值, 模 acos 反余弦 acosh 反双曲余弦 acot 反余切 acoth 反双曲余切 acsc 反余割 acsch 反双曲余割 all 所有元素均非零则为真alpha 透明控制 angle 相角 ans 最新表达式的运算结果any 有非零元则为真 area 面域图 asec 反正割 asech 反双曲正割 asin 反正弦 asinh 反双曲正弦 atan 反正切 atan2 四象限反正切 atanh 反双曲正切 autumn 红、黄浓淡色 axis 轴的刻度和表现 B b bar 直方图 binocdf 二项分布概率 binopdf 二项分布累积概率binornd 产生二项分布随机数组blanks 空格符号 bode 给出系统的对数频率曲线bone 蓝色调浓淡色阵 box 坐标封闭开关 break 终止最内循环brighten 控制色彩的明暗 butter ButterWorth低通滤波器 C c caxis (伪)颜色轴刻度 cd 设置当前工作目录 cdf2rdf 复数对角型转换到实块对角型 ceil 朝正无穷大方向取整 cell 创建单元数组 char 创建字符串数组或者将其他类型 变量转化为字符串数组 charfcn Maple函数 Children 图形对象的子对象 clabel 等高线标注 class 判别数据类别 clc 清除指令窗中显示内容 clear 从内存中清除变量和函数 clf 清除当前图形窗图形 close 关闭图形窗 collect 合并同类项 Color 图形对象色彩属性 colorbar 显示色条 colorcube 三浓淡多彩交错色 colordef 定义图形窗色彩 colormap 设置色图 comet 彗星状轨迹图 comet3 三维彗星动态轨迹线图compass 射线图;主用于方向和速度 cond 矩阵条件数 conj 复数共轭 continue 将控制转交给外层的for或while 循环 contour 等高线图

Matlab中hist函数的用法

Matlab实现频数直方图----hist的使用 “hist”是“Histogram”(直方图、柱状图)的简称。 1、N=hist(Y) 功能:将向量Y的元素平均到十个等间隔的容器中,并且返回每个容器的元素个数;如果Y是一个矩阵,hist指令逐列元素操作。 例1:执行指令 Y=[1:10]; Hist(Y) 得到: 10个蓝色方条,每个方条对应一个容器,其长度代表容器中数据的多少。由图知,容器中的数据量均为1。 例2:执行指令 Y=[1,2,2,5,6,6,8,11] Hist(Y) 得到:

Y最大为11,最小为1,故而将区间[1,11]均分为10份,分别为[1,2],(2,3],(3,4],(4,5],(5,6],(6,7],(7,8],(8,9],(9,10],(10,11]。 例3:当Y是矩阵时的情况 执行指令: Y=[1,2.5,2.1;3,3.5,6]; Hist(Y) 注意,Y为矩阵:1.0000 2.5000 2.1000 3.0000 3.5000 6.0000 Y有三列元素,逐列元素产生对应的直方图。得到:

观察此图和矩阵Y,由于Y的元素最小为1,最大为6,故而将区间[1,6]以0.5间隔划分为10个等长的子区间作为10个容器去容纳数据。图中有三种颜色的方条:蓝色、绿色和红色,分别对应Y中的第1,2,3列元素。如果第一列元素为1和3,故而区间[1,1.5]和(2.5,3]中有蓝色方条。 2、N=hist(Y,M) 功能:当M是一个标量,表明使用M个箱子。 执行指令: Y=[1,1,1.3,2.6,3,3.4,5,5.9,6,6,1,7,7,2]; Hist(Y,6) 得到: 2、N=hist(Y,X) 功能:X是向量,以X中的元素为区间中心可获得一系列区间,执行命令可获得Y在这些区间中的分布情况。

MATLAB S函数的调用实例

期末作业 1.求解微分方程???? ?????=++++=++=8102432 1.31. 232221.1x x x x x x x x x x x ,初始条件1230x x x ===。要求交mdl 文件及仿真结果的截图。 解:用Simulink 连接的仿真图如下所示: 求解出的x1,x2,x3的解如下图(a )所示的仿真结果: 2.当电源电压)30100 sin(10)(+=t t x πV 时,求解表达式y(t)的波形:

y x dt t dy 314)(-+=,其中y(0)=1。要求交mdl 文件及仿真结果的截图。 解:用Simulink 连接的仿真图如下所示: 仿真结果如下图(b )所示: 3.使用S 函数实现y=5*x+3,建立仿真模型并得出仿真结果。要求交mdl 文件,S 函数及仿真结果的截图。 解:用Simulink 连接的仿真图如下所示: S 函数的代码如下: function [sys,x0,str,ts] = mysfun3 (t,x,u,flag) switch flag, case 0, [sys,x0,str,ts]=mdlInitializeSizes;

case 1, sys=mdlDerivatives(t,x,u); case 2, sys=mdlUpdate(t,x,u); case 3, sys=mdlOutputs(t,x,u); case 4, sys=mdlGetTimeOfNextVarHit(t,x,u); case 9, sys=mdlTerminate(t,x,u); otherwise error(['Unhandled flag = ',num2str(flag)]); end function [sys,x0,str,ts]=mdlInitializeSizes sizes = simsizes; sizes.NumContStates = 0; sizes.NumDiscStates = 0; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1; sys = simsizes(sizes); x0 = []; str = []; ts = [0 0]; function sys=mdlDerivatives(t,x,u) sys = []; function sys=mdlUpdate(t,x,u) sys = []; function sys=mdlOutputs(t,x,u) sys = 5*u+3; function sys=mdlGetTimeOfNextVarHit(t,x,u) sampleTime = 1; sys = t + sampleTime; function sys=mdlTerminate(t,x,u) sys = []; 仿真结果如下图(c)所示:

相关文档
最新文档