Uploaded by Victor Okpanachi

Problem set 1 part 1 final

advertisement
%
%
%
%
Introduction to Matlab
Part 1
Run these commands in the Command Window. Use copy and paste.
Correct all errors and save your code
clear
3^2*4-3*2^5*(4-2)
% To clear all.
% Simple arithmetic.
ans = -156
calculating square root of 25
sqrt(25)
% Square root.
ans = 5
create a row Vector named x that starts at 1, ends at 9, and each element is separated by 2
x=1:2:9
x = 1×5
1
% A vector.
3
5
7
9
y=x.^2
y = 1×5
1
% All the elements of x will be squared
9
25
49
81
A=[1,2;3,4]
A = 2×2
1
3
% A 2x2 matrix.
2
4
A'
% The transpose.
ans = 2×2
1
2
3
4
det(A)
% The determinant.
ans = -2
B=[0,3,1;.3,0,0;0,.5,0]
% A 3x3 matrix.
B = 3×3
0
0.3000
0
eig(B)
3.0000
0
0.5000
1.0000
0
0
% The eigenvalues of B.
ans = 3×1
1.0230
-0.8507
-0.1724
1
[Vects,Vals]=eig(B)
Vects = 3×3
0.9506
0.2788
0.1362
Vals = 3×3
1.0230
0
0
% Eigenvectors and eigenvalues.
-0.9256
0.3264
-0.1918
0.1840
-0.3203
0.9293
0
-0.8507
0
0
0
-0.1724
C=[100;200;300]
% A 3x1 matrix.
C = 3×1
100
200
300
D=B*C
% Matrix multiplication.
D = 3×1
900
30
100
E=B^4
E = 3×3
0.8100
0.0450
0.1350
% Powers of matrices.
0.9000
0.8100
0.0750
z1=1+1i
0.1500
0.2700
0
% Complex numbers.
z1 = 1.0000 + 1.0000i
z2=1-1i
z2 = 1.0000 - 1.0000i
z3=2+1i
z3 = 2.0000 + 1.0000i
z4=2*z1-z2*z3
% Complex arithmetic.
z4 = -1.0000 + 3.0000i
abs(z1)
% Modulus.
ans = 1.4142
real(z1)
% Real part.
ans = 1
imag(z1)
% Imaginary part.
ans = 1
exp(1i*z1)
% Exponential.
2
ans = 0.1988 + 0.3096i
help exp
exp
Exponential.
exp(X) is the exponential of the elements of X, e to the X.
For complex Z=X+i*Y, exp(Z) = exp(X)*(COS(Y)+i*SIN(Y)).
See also expm1, log, log10, expm, expint.
Documentation for exp
Other functions named exp
sym(1/2)+sym(3/4)
% Symbolic arithmetic.
ans =
1/2+3/4
% Double precision.
ans = 1.2500
vpa(pi,100)
% Variable precision.
ans =
help vpa
vpa
% Shows help
Variable precision arithmetic.
R = vpa(S) numerically evaluates each element of the double matrix
S using variable precision floating point arithmetic with D decimal
digit accuracy, where D is the current setting of DIGITS.
The resulting R is a SYM.
vpa(S,D) uses D digits, instead of the current setting of DIGITS.
D is an integer or the SYM representation of a number.
It is important to avoid the evaluation of an expression using double
precision floating point arithmetic before it is passed to vpa.
For example,
phi = vpa((1+sqrt(5))/2)
first computes a 16-digit approximation to the golden ratio, then
converts that approximation to one with d digits, where d is the current
setting of DIGITS. To get full precision, use symbolic arguments,
phi = vpa((1+sqrt(sym(5)))/2)
or
s = sqrt(sym(5))
phi = vpa((1+s)/2)
Additional examples:
vpa(pi,780) shows six consecutive 9's near digit 770 in the
decimal expansion of pi.
vpa(hilb(2),5) returns
[
1., .50000]
[.50000, .33333]
See also double, digits, subs.
3
Documentation for vpa
Other functions named vpa
% Symbolic calculation
syms x(t) y(t) z(t) t
z=x^3-y^3
% Symbolic objects, declare the symbolic variabl
z(t) =
factor(z)
% Factorization.
ans(t) =
expand(6*cos(t-pi/4))
% Expansion.
ans =
simplify(z/(x-y))
% Simplification.
ans(t) =
syms x
limit(x/sin(x),x,0)
% we have to use syms for symbolic variable
% Limits.
ans =
clear
clc
syms x y
[x,y]=solve(x^2-x==0,2*x*y-y^2==0)
% Solving simultaneous equations.
x =
y =
syms x mu
f=mu*x^2*(1-x)
% Define a function.
f =
subs(f,x,1/3)
% Evaluate f(1/3).
4
ans =
fof=subs(f,x,f)
% Composite function.
fof =
diff(f,x)
% Differentiation.
ans =
syms x y
diff(x^2+3*x*y-2*y^2,y,2)
% Partial differentiation.
ans =
int(sin(x)*cos(x),x,0,pi/2)
% Integration with boundary from 0 to pi/2
ans =
int(1/x,x,0,inf)
% Improper integration.with boundary from 0 to infinity
ans =
syms n s w
s1=symsum(1/n^2,1,inf)
% Symbolic summation.
s1 =
g=exp(x)
g =
taylor(g,'Order',10)
% Taylor series up to order 10.
ans =
syms a w
laplace(x^3)
% Laplace transform.
ans =
5
ilaplace(1/(s-a))
% Inverse transform.
ans =
fourier(exp(-x^2))
% Fourier transform.
ans =
ifourier(pi/(1+w^2))
% Inverse transform.
ans =
% End of Part 1.
% Part 2
% Graph and simple differential equations
clear
% Plot a simple function.
x=-2:.01:2
%declare an array, with starting point, the step and the end point
x = 1×401
-2.0000
-1.9900
-1.9800
-1.9700
-1.9600
-1.9500
plot(x,x.^2)
6
-1.9400
-1.9300
% Plot two functions on one graph.
t=0:.1:100
t = 1×1001
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
y1=exp(-.1*t).*cos(t)
y1 = 1×1001
1.0000
0.9851
0.9607
0.9271
0.8849
0.8348
0.7773
0.7131
0.9950
0.9801
0.9553
0.9211
0.8776
0.8253
0.7648
y2=cos(t)
y2 = 1×1001
1.0000
plot(t,y1,t,y2),legend('y1','y2')
7
% Symbolic plots.
fplot(@(x) x.^2,[-2,2])
8
help fplot
fplot
Plot 2-D function
fplot(FUN) plots the function FUN between the limits of the current
axes, with a default of [-5 5].
fplot(FUN,LIMS) plots the function FUN between the x-axis limits
specified by LIMS = [XMIN XMAX].
fplot(...,'LineSpec') plots with the given line specification.
fplot(X,Y,LIMS) plots the parameterized curve with coordinates
X(T), Y(T) for T between the values specified by LIMS = [TMIN TMAX].
H = fplot(...) returns a handle to the function line object created by fplot.
fplot(AX,...) plots into the axes AX instead of the current axes.
Examples:
fplot(@sin)
fplot(@(x) x.^2.*sin(1./x),[-1,1])
fplot(@(x) sin(1./x), [0 0.1])
If your function cannot be evaluated for multiple x values at once,
you will get a warning and somewhat reduced speed:
f = @(x,n) abs(exp(-1j*x*(0:n-1))*ones(n,1));
fplot(@(x) f(x,10),[0 2*pi])
See also fplot3, fsurf, fcontour, fimplicit, plot, function_handle.
Documentation for fplot
fun = (@(t) exp(-1.*t).*sin(t))
9
fun = function_handle with value:
@(t)exp(-1.*t).*sin(t)
fplot(fun),xlabel('time'),ylabel('current'),title('decay')
% 3-D plots on a 50x50 grid.
f = @(x,y) sin(x) + cos(y);
fcontour(f)
10
help fcontour
fcontour
Plot function contour lines
fcontour(F) plots contour lines of F(X,Y) over the axes size,
with a default range of -5 < X < 5, -5 < Y < 5.
fcontour(F,[XYMIN XYMAX]) plots over XYMIN < X < XYMAX, XYMIN < Y < XYMAX.
fcontour(F,[XMIN XMAX YMIN YMAX]) plots over XMIN < X < XMAX, YMIN < Y < YMAX.
H = fcontour(...) returns a handle to the function contour object created by fcontour.
fcontour(...,'LineSpec') plots with the given line specification.
fcontour(AX,...) plots into the axes AX instead of the current axes.
Examples:
fcontour(@(x,y) x.^2+y.^2)
fcontour(@(x,y) sin(x).*cos(y),[-2*pi,2*pi],'MeshDensity',121)
See also contour, fimplicit, fplot, fplot3, fsurf, function_handle.
Documentation for fcontour
fcontour(@(x,y) sin(x) + cos(y))
11
f1 = @(x,y) y.^2/2-x.^2/2+x.^4/4
f1 = function_handle with value:
@(x,y)y.^2/2-x.^2/2+x.^4/4
fcontour(f1,[-2 2],'MeshDensity',50)
12
fsurf(f1,[-2 2],'MeshDensity',50)
13
help fsurf
fsurf
Plot 3-D surface
fsurf(FUN) creates a surface plot of the function FUN(X,Y). FUN is plotted over
the axes size, with a default interval of -5 < X < 5, -5 < Y < 5.
fsurf(FUN,INTERVAL) plots FUN over the specified INTERVAL instead of the
default interval. INTERVAL can be the vector [XMIN,XMAX,YMIN,YMAX] or the
vector [A,B] (to plot over A < X < B, A < Y < B).
fsurf(FUNX,FUNY,FUNZ) plots the parametric surface FUNX(U,V),
FUNY(U,V), and FUNZ(U,V) over the interval -5 < U < 5 and
-5 < V < 5.
fsurf(FUNX,FUNY,FUNZ,[UMIN,UMAX,VMIN,VMAX]) or
fsurf(FUNX,FUNY,FUNZ,[A,B]) uses the specified interval.
fsurf(AX,...) plots into the axes AX instead of the current axes.
H = fsurf(...) returns a handle to the surface object in H.
Examples:
fsurf(@(x,y) x.*exp(-x.^2-y.^2))
fsurf(@(x,y) besselj(1,hypot(x,y)))
fsurf(@(x,y) besselj(1,hypot(x,y)),[-20,20]) % this can take a moment
fsurf(@(x,y) sqrt(1-x.^2-y.^2),[-1.1,1.1])
fsurf(@(x,y) x./y+y./x)
fsurf(@peaks)
f = @(u) 1./(1+u.^2);
fsurf(@(u,v) u, @(u,v) f(u).*sin(v), @(u,v) f(u).*cos(v),[-2 2 -pi pi])
A = 2/3;
B = sqrt(2);
xfcn = @(u,v) A*(cos(u).*cos(2*v) + B*sin(u).*cos(v)).*cos(u) ./ (B - sin(2*u).*sin(3*v));
yfcn = @(u,v) A*(cos(u).*sin(2*v) - B*sin(u).*sin(v)).*cos(u) ./ (B - sin(2*u).*sin(3*v));
zfcn = @(u,v) B*cos(u).^2 ./ (B - sin(2*u).*sin(3*v));
h = fsurf(xfcn,yfcn,zfcn,[0 pi 0 pi]);
If your function has additional parameters, for example k in myfun:
%------------------------------%
function z = myfun(x,y,k1,k2,k3)
z = x.*(y.^k1)./(x.^k2 + y.^k3);
%------------------------------%
then you may use an anonymous function to specify that parameter:
fsurf(@(x,y)myfun(x,y,2,2,4))
See also fplot, fplot3, fmesh, fimplicit3, surf, vectorize, function_handle.
Documentation for fsurf
fsurf(f1,[-3 3],'ShowContours','on')
14
% Parametric plot.
ezplot('t^3-4*t','t^2',[-3,3])
15
fplot(@(t) t.^2+4*t,@(t) t.^2,[-5,2])
16
% 3-D parametric plot.
ezplot3('sin(t)','cos(t)','t',[-10,10])
% Symbolic solutions to ODEs.
%dsolve('Dx=-x/t')
syms x(t)
dsolve(diff(x)==-x/t)
ans =
help dsolve
dsolve Symbolic solution of ordinary differential equations.
dsolve will not accept equations as strings in a future release.
Use symbolic expressions or sym objects instead.
For example, use syms y(t); dsolve(diff(y)==y) instead of dsolve('Dy=y').
dsolve(eqn1,eqn2, ...) accepts symbolic equations representing
ordinary differential equations and initial conditions.
By default, the independent variable is 't'. The independent variable
may be changed from 't' to some other symbolic variable by including
that variable as the last input argument.
The DIFF function constructs derivatives of symbolic functions (see sym/symfun).
Initial conditions involving derivatives must use an intermediate
variable. For example,
17
syms x(t)
Dx = diff(x);
dsolve(diff(Dx) == -x, Dx(0) == 1)
If the number of initial conditions given is less than the
number of dependent variables, the resulting solutions will obtain
arbitrary constants, C1, C2, etc.
Three different types of output are possible. For one equation and one
output, the resulting solution is returned, with multiple solutions to
a nonlinear equation in a symbolic vector. For several equations and
an equal number of outputs, the results are sorted in lexicographic
order and assigned to the outputs. For several equations and a single
output, a structure containing the solutions is returned.
If no closed-form (explicit) solution is found, then a
warning is given and the empty sym is returned.
dsolve(...,'IgnoreAnalyticConstraints',VAL) controls the level of
mathematical rigor to use on the analytical constraints of the solution
(branch cuts, division by zero, etc). The options for VAL are TRUE or
FALSE. Specify FALSE to use the highest level of mathematical rigor
in finding any solutions. The default is TRUE.
dsolve(...,'MaxDegree',n) controls the maximum degree of polynomials
for which explicit formulas will be used in SOLVE calls during the
computation. n must be a positive integer smaller than 5.
The default is 2.
dsolve(...,'Implicit',true) returns the solution as a vector of
equations, relating the dependent and the independent variable. This
option is not allowed for systems of differential equations.
dsolve(...,'ExpansionPoint',a) returns the solution as a series around
the expansion point a.
dsolve(...,'Order',n) returns the solution as a series with order n-1.
Examples:
% Example 1
syms x(t) a
dsolve(diff(x) == -a*x) returns
ans = C1/exp(a*t)
% Example 2: changing the independent variable
x = dsolve(diff(x) == -a*x, x(0) == 1, 's') returns
x = 1/exp(a*s)
syms x(s) a
x = dsolve(diff(x) == -a*x, x(0) == 1) returns
x = 1/exp(a*s)
% Example 3: solving systems of ODEs
syms f(t) g(t)
S = dsolve(diff(f) == f + g, diff(g) == -f + g,f(0) == 1,g(0) == 2)
returns a structure S with fields
S.f = (i + 1/2)/exp(t*(i - 1)) - exp(t*(i + 1))*(i - 1/2)
S.g = exp(t*(i + 1))*(i/2 + 1) - (i/2 - 1)/exp(t*(i - 1))
18
syms f(t) g(t)
v = [f;g];
A = [1 1; -1 1];
S = dsolve(diff(v) == A*v, v(0) == [1;2])
returns a structure S with fields
S.f = exp(t)*cos(t) + 2*exp(t)*sin(t)
S.g = 2*exp(t)*cos(t) - exp(t)*sin(t)
% Example 3: using options
syms y(t)
dsolve(sqrt(diff(y))==y) returns
ans = 0
syms y(t)
dsolve(sqrt(diff(y))==y, 'IgnoreAnalyticConstraints', false) warns
Warning: The solutions are subject to the following conditions:
(C67 + t)*(1/(C67 + t)^2)^(1/2) = -1
and returns
ans = -1/(C67 + t)
% Example 4: Higher order systems
syms y(t) a
Dy = diff(y);
D2y = diff(y,2);
dsolve(D2y == -a^2*y, y(0) == 1, Dy(pi/a) == 0)
syms w(t)
Dw = diff(w);
D2w = diff(w,2);
w = dsolve(diff(D2w) == -w, w(0)==1, Dw(0)==0, D2w(0)==0)
See also solve, subs, sym/diff, odeToVectorField.
Documentation for dsolve
dsolve('D2x+5*Dx+6*x=10*sin(t)','x(0)=0','Dx(0)=0')
Warning: Support of character vectors and strings will be removed in a future release. Use sym objects to
define differential equations instead.
ans =
clear
clc
syms x(t)
dsolve(diff(x,t) == -x/t, x(1) == 1)
ans =
% Linear systems of ODEs.
syms x(t) y(t)
19
eqns = [diff(x,t) == 3*x+4*y, diff(y,t) == -4*x+3*y]
eqns(t) =
S = dsolve(eqns)
S = struct with fields:
y: [1×1 sym]
x: [1×1 sym]
ySol(t) = S.y
ySol(t) =
[x,y]=dsolve('Dx=x^2','Dy=y^2','x(0)=1,y(0)=1')
Warning: Support of character vectors and strings will be removed in a future release. Use sym objects to
define differential equations instead.
x =
y =
% A 3-D linear system.
syms x(t) y(t) z(t) t
[x,y,z]=dsolve('Dx=x','Dy=y','Dz=-z')
Warning: Support of character vectors and strings will be removed in a future release. Use sym objects to
define differential equations instead.
x =
y =
z =
%[x,y,z]=dsolve(diff(x,t)==x,diff(y,t)==y,diff(z,t)==z)
% Numerical solutionms to ODEs.
deq1=@(t,x) x(1)*(.1-.01*x(1))
deq1 = function_handle with value:
@(t,x)x(1)*(.1-.01*x(1))
[t,xa]=ode45(deq1,[0 100],50)
t = 65×1
0
0.1256
0.2512
0.3768
0.5024
0.8124
20
1.1225
1.4325
1.7425
2.1446
xa = 65×1
50.0000
47.6223
45.4864
43.5576
41.8067
38.0987
35.0903
32.6095
30.5149
28.2294
plot(t,xa(:,1))
% A 2-D system.
deq2=@(t,x) [.1*x(1)+x(2);-x(1)+.1*x(2)]
deq2 = function_handle with value:
@(t,x)[.1*x(1)+x(2);-x(1)+.1*x(2)]
[t,xb]=ode45(deq2,[0 50],[.01,0])
t = 245×1
21
0
0.0050
0.0100
0.0151
0.0201
0.0452
0.0703
0.0955
0.1206
0.2462
xb = 245×2
0.0100
0.0100
0.0100
0.0100
0.0100
0.0100
0.0100
0.0100
0.0100
0.0099
0
-0.0001
-0.0001
-0.0002
-0.0002
-0.0005
-0.0007
-0.0010
-0.0012
-0.0025
plot(xb(:,1),xb(:,2))
% A 3-D system.
deq3=@(t,x) [x(3)-x(1);-x(2);x(3)-17*x(1)+16]
22
deq3 = function_handle with value:
@(t,x)[x(3)-x(1);-x(2);x(3)-17*x(1)+16]
[t,xc]=ode45(deq3,[0 20],[.8,.8,.8])
t = 345×1
0
0.0126
0.0251
0.0377
0.0502
0.1046
0.1590
0.2134
0.2678
0.3281
xc = 345×3
0.8000
0.8003
0.8010
0.8023
0.8040
0.8172
0.8391
0.8686
0.9042
0.9490
0.8000
0.7900
0.7802
0.7704
0.7608
0.7205
0.6824
0.6462
0.6120
0.5762
0.8000
0.8404
0.8813
0.9224
0.9637
1.1425
1.3146
1.4717
1.6065
1.7227
plot3(xc(:,1),xc(:,2),xc(:,3))
23
% A stiff system.
deq4=@(t,x) [x(2);1000*(1-(x(1))^2)*x(2)-x(1)]
deq4 = function_handle with value:
@(t,x)[x(2);1000*(1-(x(1))^2)*x(2)-x(1)]
[t,xd]=ode23s(deq4,[0 3000],[.01,0])
t = 1086×1
103 ×
0
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
xd = 1086×2
103 ×
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
plot(xd(:,1),xd(:,2))
24
% x versus t.
plot(t,xd(:,1))
25
% End of Part 2.
26
Download