% Prompt the user for the input power. pin = input('Enter the power in watts: ');
% Calculate dBm
dBm = 10 * log10( pin / 1.0e-3 );
% Tell user
disp (['Power = ' num2str(dBm) ' dBm']); 程序2: %
% Define variables:
% dBm -- Power in dBm % pin -- Power in watts
% Create array of power in watts pin = 1:2:100;
% Calculate power in dBm
dBm = 10 * log10( pin / 1.0e-3 );
% Plot on linear scale figure(1); plot(dBm,pin);
title('Plot of power in watts vs power in dBm');
xlabel('Power (dBm)'); ylabel('Power (watts)'); grid on;
% Plot on semilog scale figure(2);
semilogy(dBm,pin);
title('Plot of power in watts vs power in dBm');
xlabel('Power (dBm)'); ylabel('Power (watts)'); grid on;
Current Through a Diode. (Suggestion: you had better use the function file )
Figure. A semiconductor diode.
The current flowing through the semiconductor diode(半导体二极管) shown in Figure is given by the equation
qVD
iD?I0(ekT?1)
Where
VD = the voltage across the diode, in volts iD = the current flow through the diode ,in amps
I0 = the leakage current of the diode, in amps.
11
q = the charge on an electron, 1.602×10-19coulombs(库仑)
k = Boltzmann's constant, 1.38×10-23joule/k T = temperature, in kelvins (K)
The leakage current I0 of the diode is 2.0uA (Hint:2×10-6A). Write a program to calculate the current flowing through this diode for all voltages from -1.0 V to +0.6V, in 0.1Vsteps. Repeat this process for the following temperatures: 750F, 1000F and 1250F. Create a plot of the current as a function of applied voltage, with the curves for three different temperatures appearing as different colors.
(Hint:The formula for the conversion from temperature in degrees Fahrenheit to absolute temperature in kelvins:
T(inKelvins)?(59T(in?F)?32.0)?273.15 )
% Define variables:
% i0 -- Leakage current (A) % id -- Diode current (A) % k -- Boltzmann's constant (joule/K)
% q -- Charge on an electron (coul)
% temp_f -- Temperature (deg F) % temp_k -- Temperature (K) % vd -- Diode voltage (V)
% Initial values
i0 = 2.0e-6; % amps k = 1.38e-23; % joule/K q = 1.602e-19; % Coulombs vd = -1.0:0.01:0.6; % Volts temp_f = [75 100 125]; % def F for ii = 1:length(temp_f)
% Convert temperature to kelvins. temp_k = (5/9) * (temp_f(ii) - 32) + 273.15;
% Calculate currents
id = i0 .* ( exp((q*vd)/(k*temp_k)) - 1 ); % Plot line in various colors if ii == 1
plot(vd,id,'b-','LineWidth',2); hold on; elseif ii == 2
plot(vd,id,'k--','LineWidth',2); elseif ii == 3
plot(vd,id,'r:','LineWidth',2); hold off; end end
legend('75 F', '100 F', '125 F') grid on;
title('\\bfPlot of diode voltage vs diode current');
xlabel('\\bf\\itv_{D}'); ylabel('\\bf\\iti_{D}');
1、Fibonacci numbers. The nth Fibonacci number is defined by the following recursive equations: f(1)=1 f(2)=2
f(n)=f(n-1)+f(n-2)
Therefore, f(3)=f(2)+f(1)=2+1=3, and so forth for higher numbers. Write an M-file to calculate and write out the nth Fibonacci number for n>2, where n is input by the user. Use a while loop to perform the calculation.
% Define variables:
% fn -- Fibonacci number
% n -- The item in the sequence to calculate
% Get n
n = input('Enter the Fobonacci number n to evaluate (n>2): ');
% Check to see that n is an integer greater than two if n <= 2
disp('Error--n must greater than two!');
elseif round(n) ~= n
disp('Error--n must be an integer!'); else
12
% Calculate fn fn = zeros(1,n); fn(1) = 1; fn(2) = 2; ii = 3;
while ii <= n;
fn(ii) = fn(ii-1) + fn(ii-2); ii = ii + 1; end
% Display result
disp(['The ' int2str(n) 'th Fibonacci number = ' int2str(fn(n))]); end
2、Geometric Mean. The geometric mean of a set of numbers x1 through xn is defined as the nth root of the product of the numbers Geometric mean=n(x1x2x3..xn)
Write a MATLAB program that will accept an arbitrary number of positive input values and calculate both the arithmetic mean(i.e, the average) and the geometric mean of the numbers. Use a while loop to get the input values and terminate the inputs when a user enters a negative number. Test your program by calculating the average and geometric mean of the four numbers 10, 5, 2, and 5.
% Define variables:
% x -- Input values % g -- Geometric mean
% nvals -- Number of input values % prod -- Product of input values
% Initialize product and nvals prod = 1; nvals = 0;
% Read the first number
x = input('Enter first number: ');
% Read the remaining numbers while x > 0
prod = prod * x; nvals = nvals + 1;
x = input('Enter next number: '); end
% Calculate geometric mean g = prod ^ (1/nvals);
% Tell user
fprintf('The geometric mean is %.4f\\n',g);
3、Harmonic Mean. The harmonic mean is yet another way of calculating a mean for a set of numbers. The harmonic mean of a set of numbers is given by the equation
ha
N1
x?1?...?11x2xNWrite a MATLAB program that will read in an arbitrary number of positive input values and calculate the harmonic mean of the numbers. Use any method that you desire to read in the input values. Test your program by calculating the harmonic mean of the four numbers 10, 5, 2 and 5.
% Define variables: % hmean -- Harmonic mean
% nvals -- Number of input values
% sumr -- Sum of reciprocals of the input values
% x -- Input values
% Initialize sum sumr = 0;
% Read the number of input values
nvals = input('Enter number of values: ');
% Read the numbers for ii = 1:nvals
x = input('Enter number: '); sumr = sumr + 1/x; end
% Calculate harmonic mean
13
hmean = nvals / sumr;
% Tell user fprintf('The harmonic
mean
is %.4f\\n',hmean); aa=input('The number:','s'); b=str2num(aa); n=length(b); prod=1; for ii=1:n
prod=prod*b(ii); end
9、12、13、14 in textbook P57. Reference answer:
chapter3_9
% Define variables:
% dist1AB -- Distance between A point and B point
% dist2BC -- Distance between B point and C point
% dist3AC -- Distance between A point and C point
% Ax1, Ay1, Az1 -- Point A % Bx2, By2, Bz2 -- Point B % Cx3, Cy3, Cz3 -- Point C
% Prompt the user for the input points Ax1=input('Ax1: ');
Ay1=input('Ay1: '); Az1=input('Az1: '); Bx2=input('Bx2: '); By2=input('By2: '); Bz2=input('Bz2: '); Cx3=input('Cx3: '); Cy3=input('Cy3: '); Cz3=input('Cz3: '); % Calculate the distance
dist1AB=sqrt((Ax1-Bx2)^2+(Ay1-By2)^2+(Az1-Bz2)^2);
dist2BC=sqrt((Bx2-Cx3)^2+(By2-Cy3)^2+(Bz2-Cz3)^2);
dist3AC=sqrt((Ax1-Cx3)^2+(Ay1-Cy3)^2+(Az1-Cz3)^2);
% Tell user
disp (['The distance AB is ' num2str(dist1AB)]); disp (['The distance BC is ' num2str(dist2BC)]); disp (['The distance AC is ' num2str(dist3AC)]);
chapter3_12
Tfine variables: x=-5:0.1:5; y1=zeros(size(x)); y2=zeros(size(x)); y3=zeros(size(x)); N=length(x); for k=1:N if x(k)>1 y1(k)=1;
elseif x(k)<=1&x(k)>=-1
y2(k)=x(k); else y3(k)=-1; end end
y=y1+y2+y3; plot(x,y)
chapter3_13
非循环
clc;
tic,t=cputime; i=0:1:63; k=sum(2.^i)
%disp(['k=1+2+2^2+2^3+...+2^62+2^63= ' num2str(k)]); toc,cputime-t;
for循环
tic,t=cputime; k=0; for i=0:63 k=k+2.^i; end k
toc,cputime-t;
14
while 循环
tic,t=cputime; sum=0; i=0; while 1 sum=sum+2.^i;
i= i+1; if i> 63 break; end end sum
toc,cputime-t;
chapter3_14
Tfine variables:
% t--the equation involved
%x1--temp value of x in order to find the value of x
%x2--temp value of y in order to find the value of x
%y1--temp value of x in order to find the value of y
%y2--temp value of y in order to find the value of y
%initialize t
t=input('please enter the value of the t :') %get the initial values of x1 and x2
x1=input('please enter the value of the x1 :') x2=input('please enter the value of the x2 :') %count the value of x by loops for i=1:32
y1=log(x1)-cos(x1+t); y2=log(x2)-cos(x2+t); x=0.5*(x1+x2); y=log(x)-cos(x); if y*y1>0
x1=x; end if y*y2>0
x2=x; end end
format long; x,y
function [x,esterr]=equval(phi)
x1=0.2;x2=3; for ii=1:50
y1=log(x1)-cos(x1+phi); y2=log(x2)-cos(x2+phi); x=0.5*(x1+x2); y=log(x)-cos(x+phi); if y*y1>0, x1=x;end if y*y2>0, x2=x;end end
esterr=log(x)-cos(x+phi); format long;
fprintf('\\nWhen PHI is ');
disp(sym(phi));
fprintf('Value of x is %3.4f,',x);
fprintf('Error of the solution is %e.\\n',esterr);
>>phi=0:pi/8:3*pi/8; x=zeros(1,4); for k=1:4
x(k)=equval(phi(k)); end The output is: When PHI is 0
Value of x is 1.3030,Error of the solution is 2.053913e-015.
When PHI is pi/8
Value of x is 1.0909,Error of the solution is -9.575674e-016.
When PHI is pi/4
Value of x is 0.8957,Error of the solution is -1.332268e-015.
When PHI is 3*pi/8
Value of x is 0.7230,Error of the solution is -3.608225e-015.
一 matlab基础知识
1 数值的表示
Matlab的数只采用习惯的十进制表示,可以带小数点和负号;其缺省的数据类型为双精度浮点型(double)。
15
例如:3 -10 0.001 1.3e10 1.256e-6 变量命令规则
变量名、函数名对字母的大小写是敏感的。如myVar与myvar表示两个不同的变量。 变量名第一个字母必须是英文字母。
变量名可以包含下英文字母、划线和数字。 变量名不能包含空格、标点。
变量名最多可包含63个字符(6.5及以后的版本) 2 数组的分类
一维数组,也称为向量(vector) 。
行向量(row vector)、列向量(column vector)。 二维数组(矩阵matrix)。 多维数组。
有效矩阵:每行元素的个数必须相同,也就是说其每列元素的个数耶也必须相同。 (1)创建一维数组变量
方法1 使用方括号“[ ]”操作符。 例如 A[1 2 3] 方法2 使用冒号“:”操作符 例如 B=1:10 方法3 利用函数linspace 例如 a=linspace(1 ,2, 5)方法4 利用函数logspace
方法5 (3) 利用已有的矩阵剪裁方法创建向量。 列向量的创建
使用方括号“[ ]”操作符,使用分号“;”分割行 使用冒号操作符
创建数组变量的一般方法
创建变量的赋值语句的一般格式 var=expression var为变量名
expression为MATLAB合法表达式 可以是单独的常数值或数值数组;
也可以由常数值、其他变量(部分或全部)、数值数组