当前位置:
文档之家› Le-(7)matlab编程习题
Le-(7)matlab编程习题
2009-4-13
11
Computer Simulation & Application
5、sinc函数 与 Sa(t)=sin(t)/t t=linspace(-5,5); y1=sinc(t); Subplot(121) plot(t,y1); y2=sin (t)./t; Subplot(122) plot(t,y2);
2009-4-13
调用方法: Sum=mysum(0,15) %【s=65535】
用while循环 sum=0,i=n1; while i<=n2 sum=sum+2^i; i=i+1; end
5
Computer Simulation & Application
3、对一个变量 x 自动赋值。当从键盘输入 y 或 Y 时(表示是),x 自动赋为 1; 当从键盘输入 n 或 N 时(表示否),x 自动赋为 0;输入其他字符 时显示’wrong character’。编写函数文件。
2009-4-13
2
Hale Waihona Puke Computer Simulation & Application
一、程序文件
主程序文件 函数文件
2009-4-13
3
Computer Simulation & Application
1、编写主程序(命令)文件:计算 1+2+…+n<2000 时的最大 n 值 分析:累加,故只 能用循环 n=1; s=1; while s<2000 n=n+1; s=s+n; end n-1
2009-4-13 18
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 3、求函数零点 fzero
X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0, if X0 is a scalar. 例: X = fzero(@sin,3) X = fzero(@(x) sin(3*x),2) Or function f = myfun(x,c) f=cos(c*x); c = 2; % define parameter first x = fzero(@(x) myfun(x,c),0.1)
2009-4-13
验证程序: s=1; for i=2:62; s=s+i; end s % 【s=1953】 若改成函数文件呢? 例:任意参数SUM,编写函数 文件my_max_sum,求出使 1+2+…+n<SUM的n值
4
Computer Simulation & Application
2、编写函数文件:分别用 for 和 while 循环编写 程序,求 2 的 n1 到 n2 次幂的和
调用方法: x=autox
disp('wrong character')
2009-4-13
6
Computer Simulation & Application
4、编写分段函数 的函数文件,存放于文件ff.m 中,计算出 f (−3), f ( 2), f (∞) 的值。 function y=f(x) if x>=0 & x<1 y=x; y=2-x;
2009-4-13 19
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 4、求定积分 quad (or quad8)
Q = QUAD(FUN,A,B) tries to approximate the integral of scalar-valued function FUN from A to B to within an error of 1.e-6 using recursive adaptive Simpson quadrature. Example: Q = quad(@myfun, 0, 2 ); where myfun.m is the M-file function: %-------------------% function y = myfun(x) y = 1./(x.^3-2*x-5); %-------------------%
2009-4-13
8
Computer Simulation & Application
二、常见信号的产生和绘制
例:连续时间信号 f(t)=Sa(t)=sin(t)/t t= -10:1.5:10;f=sin(t)./t; plot(t,f); title(‘f(t)=Sa(t)’); xlabel(‘t’); axis([-10,10,-0.4,1.1]);
2009-4-13 17
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 2、求函数极值 fminbnd
X = FMINBND(FUN,x1,x2) attempts to find a local minimizer X of the function FUN in the interval x1 < X < x2. 例: X = fminbnd(@cos,3,4) x = fminbnd(@(x) sin(x)+3,2,5) Or function f = myfun(x,c) f = (x - c)^2; c = 1.5; % define parameter first x = fminbnd(@(x) myfun(x,c),0,1)
2009-4-13
14
Computer Simulation & Application
任意函数的定义方法(一)
例:f=@(t)(sin(pi/4*t)); % f= sin(pi/4*t) 例: a=1;b=-2;c=1; f=@(x)(a*x.^2+b*x+c); ezplot(f) 例:a=pi,b=15; f=@(x,y)(a*x+b*y); ezsurf(f)
Computer Simulation & Application
计算机仿真及应用
MATLAB编程
主讲教师:魏广芬 山东工商学院信息与电子工程学院 eegingkowei@
2009-4-13 1
Computer Simulation & Application
主要内容
一、程序文件 二、常见信号的产生和绘制
2009-4-13 12
% y1 = sin(pi*t)/(pi*t)
Computer Simulation & Application
一、向量表示法
对于连续时间信号f(t),可以用两个行向量f和t来表 示,其中向量t是形如t=t1:p:t2的MATLAB命令定义 的时间范围向量,t1为信号起始时间,t2为信号终止 时间,p为时间间隔。向量f为连续信号f(t)在向量t所 定义的时间点上的样值。 时间间隔p取得小,信号波形光滑
2009-4-13
10
Computer Simulation & Application
3、周期方波
t=0:0.001:2.5; y=square(2*pi*30*t); plot(t(1:50),y(1:50));
4、周期锯齿波
t=0:0.001:2.5; y=sawtooth(2*pi*30*t); plot(t,y); axis([0 0.2 –1 1]);
调用方法: ff(-3) %0
⎧ x, ⎪ f ( x ) = ⎨ 2 − x, ⎪ 0, ⎩
0 ≤ x <1 1≤ x < 2 others
elseif x>=1 & x<2 else end y=0;
ff(sqrt(2)) % 0.5858 ff(inf)
2009-4-13
%0
7
Computer Simulation & Application
function [sum]=mysum(n1,n2) %%% sum=mysum(n1,n2) %%% n1: the first number, %%% n2: the last number %%% sum=2^n1+2^(n1+1)+...+2^ (n2) sum=0; for i=n1:n2 sum=sum+2^i; end
2009-4-13 20
Computer Simulation & Application
4.4 函数功能和数值积分函数库(funfun) 5、求不定积分 利用定积分函数求不定积分 例: s=quad(‘humps’,1,2) %[1,2]之间的定积分 求humps函数以x=0为下限的不定积分
for i=1:20 x(i)=0.1*i; y(i)=quad('humps',0,x(i)); end plot(x,y)
2009-4-13
13
Computer Simulation & Application
二、 ezplot
如果信号可以用一个符号表达式来表示它,则我们 可用ezplot命令(缺省的区间为[-2*pi,2*pi])绘制出 信号的波形,例如对于连续信号f(t)=sin(πt/4) ezplot(‘sin(pi/4*t)’); ezplot('sin(pi/4*t)',[-16,16]);
function x=autox %%% x is 1, if input y or Y; x is 0, if input n or N;%%% break if input other characters a=input('input a character: ','s'); if a=='y' | a=='Y' elseif a=='n' | a=='N' else end x=1; x=0;