This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Matlab Maths Practice
1.Solving AlgebraicEquations and Other Symbolic Tools Solving Basic Algebraic Equations
1.x + 3 = 0
Command window
>> x=solve('x+3=0')
x =
-3
2. x + 8 =0
Command window
>> x=solve('x+8')
x =
-8
3.ax + 5 = 0
>> solve('a*x+5','a')
ans =
-5/x
>> solve('a*x+5','x')
ans =
-5/a
2.Solving Quadratic Equations
x2 6x 12 = 0
Command window
s=solve('x^2-6*x-12=0')
s =
3+21^(1/2)
3-21^(1/2)
3.Plotting Symbolic Equations
1.x^2-6*x-12
command window
d=('x^2-6*x-12');
ezplot(d)
same equation with a example
d='x^2-6*x-12'
command window
>> d='x^2-6*x-12'
d =
x^2-6*x-12
>> ezplot(d,[2,8])
2.we wanted to plot:
x + 3 = 0
4 < x < 4, 2 < y < 2
Command window
>> ezplot('x+3',[-4 4 -2 2])
>>
Output
3.Find the roots of x3 + 3x2 2x 6 and plot the function for �8 < x < 8, �8 < y < 8. Generate the plot with a grid.
command window
>> f='x^3+3*x^2-2*x-6'
f =
x^3+3*x^2-2*x-6
>> s=solve(f)
s =
-3
2^(1/2)
-2^(1/2)
>> ezplot(f,[-8 8,-8,8]),grid on
>>
Graph
4.Systems of Equations
1. 5x + 4y = 3 x - 6y = 2
Command window
>> s=solve('5*x+4*y=3','x-6*y=2');
>> x=s.x
x =
13/17
>> y=s.y
y =
-7/34
2.
w + x + 4y + 3z = 5
2w + 3x + y 2z = 1
w + 2x 5y + 4z = 3
w-3z = 9
command window
>> eq1=('w+x+4*y+3*z=5')
eq1 =
w+x+4*y+3*z=5
>> eq2=('2*w+3*x+y-2*z=1')
eq2 =
2*w+3*x+y-2*z=1
>> eq3=('w+2*x-5*y+4*z=3')
eq3 =
w+2*x-5*y+4*z=3
>> eq4=('w-3*z=9')
eq4 =
w-3*z=9
>> s=solve(eq1,eq2,eq3,eq4)
s =
w: [1x1 sym]
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
>> w=s.w
w =
1404/127
>> x=s.x
x =
-818/127
>> y=s.y
y =
-53/127
>> z=s.z
z =
87/127
5. Expanding and Collecting Equations
expand
(x + 2) (x 3) = x2 x 6
command window
>> syms x
>> expand((x-1)*(x+4))
ans =
x^2+3*x-4
>> syms y
>> expand(cos(x+y))
ans =
cos(x)*cos(y)-sin(x)*sin(y)
>> expand(sin(x-y))
ans =
sin(x)*cos(y)-cos(x)*sin(y)
>> expand((y-2)*(y+6))
ans =
y^2+4*y-12
>>
collect
>> collect(x*(x^2-2))
ans =
x^3-2*x
>> syms t
>> collect((t+3)*sin(t))
ans =
sin(t)*t+3*sin(t)
Factors
>> factor(x^2-y^2)
ans =
(x-y)*(x+y)
Solving with Exponential and Log Functions
Find a value of x that satisfi es:
log10 (x) log10 (x 3) = 1
command window
>> eq='log10(x)-log10(x-3)=1';
>> s=solve(eq)
s =
10/3
Series Representations of Functions
>> syms x
>> s=taylor(sin(x))
s =
x-1/6*x^3+1/120*x^5
>> ezplot(s)
Computing Derivatives(Important)
>> syms x t
>> f=x^2;
>> g=sin(10+t);
>> diff(f)
ans =
2*x
>> diff(sin(10+t))
ans =
cos(10+t)
To take higher derivatives of a function f, we use the syntax diff(f,n).
>> syms x
>> f=x^4;
>> diff(f,2)
ans =
12*x^2
Show that f (x) x2 satisfies -(df/dx)+2*x=0
>> syms x
>> f=x^2; g=2*x;
>> h=diff(f);
>> -h+g
ans =
0
Does y (t) 3 sin t 7 cos 5t solve y'' y –5 cos 2t?
>> syms t
>> y=3*sin(t)+7*cos(5*t);
>> z=diff(y,2);
>> x=z+y
x =
-168*cos(5*t)
As from the results we can deduce that y(t) cannot solve the above required equation.
Find the minima and maxima of the function f (x) x3 – 3x2 3x in the
interval
[0, 2].
>> syms x
>> f=x^3-3x^2+3*x;
>> f=x^3-3*x^2+3*x;
>> ezplot(f,[0,2])
>> g=diff(f)
g =
3*x^2-6*x+3
>> pretty(g)
2
3 x - 6 x + 3
>> s=solve(g)
s =
1
1
We see that there is only one critical point, since the derivative has a double root.
We can see from the plot that the maximum occurs at the endpoint, but let’s prove
this by evaluating the function at the critical points x 0, 1, 2.
So let’s check f for x 0, 1, 2. We can check all three on a single line and
have MATLAB report the output by passing a comma-delimited list:
Since f (2) returns the largest value, we conclude that the maximum occurs at
x 0. For fun, let’s evaluate the derivative at these three points and plot it:
>> subs(g,0),subs(g,1),subs(g,2)
ans =
3
ans =
0
ans =
3
Where are the critical points of the derivative? We take the second derivative and
set equal to zero:
>> subs(g,0),subs(g,1),subs(g,2)
ans =
3
ans =
0
ans =
3
>> h=diff(g)
h =
6*x-6
>> solve(h)
ans =
1
>> y=diff(h)
y =
6
>>
Plot the function f (x) x4 –2x3 and show any local minima and maxima.
>> syms x
>> f=x^4-2*x^3;
>> ezplot(f,[-2,3]);
>> g=diff(f)
g =
4*x^3-6*x^2
>> s=solve(g)
s =
3/2
0
0
>> h=diff(g)
h =
12*x^2-12*x
>> subs(h,3/2),subs(h,0),subs(h,0)
ans =
9
ans =
0
ans =
0
>>
INTEGRATION
Find int3y sec(x) dy .
command window
>> syms x y
>> int('3*y^2*sec(x)',y)
ans =
y^3*sec(x)
>> a=int('3*y^2*sec(x)',x)
a =
3*y^2*log(sec(x)+tan(x))
>> pretty(a)
2
3 y log(sec(x) + tan(x))
Definite Integration The int command can be used for definite integration by passing the limits over which you want to calculate the integral.
>> int('x',2,3)
ans =
5/2
>> b=int('x^2*cos(x)',-6,6)
b =
68*sin(6)+24*cos(6)
>> double(b)
ans =
4.0438
Matlab Code
>> syms x
>> a=int(exp(-x^2)*sin(x),o,inf)
??? Undefined function or variable 'o'.
>> a=int(exp(-x^2)*sin(x),0,inf)
a =
1/2*pi^(1/2)*erfi(1/2)*exp(-1/4)
>> pretty(a)
1/2
1/2 pi erfi(1/2) exp(-1/4)
>> double(a)
ans =
0.4244
Matlab code
>> syms x
>> int(pi*exp(-2*x),1,2)
ans =
1/2*pi*exp(-2)-1/2*pi*exp(-4)
>> f=1/2*pi*exp(-2)-1/2*pi*exp(-4)
f =
0.1838
Matlab Code
>> syms x y z
>> int(int(int(x*y^2*z^5,x),y),z)
ans =
1/36*x^2*y^3*z^6
Matlab Code
f =
x^2*y
>> int(int(f,x,2,4),y,1,2)
ans =
28
Matlab Code
>> syms r theta z h a
>> v=int(int(int(r,0,h),theta,0,2*pi),z,0,a)
v =
h^2*pi*a
>> subs(v,{a,h},{3.5,5})
ans =
274.8894
>> syms r theta z h a
>> v=int(int(int(r,0,h),theta,0,2*pi),z,0,a)
v =
h^2*pi*a
>> subs(v,{a,h},{3.5,5})
ans =
274.8894
Transforms
Transforms, such as the Laplace, z, and Fourier transforms, are used throughout science and engineering. Besides simplifying the analysis, transforms allow us to see data in a new light. For example, the Fourier transform allows you to view a signal that was represented as a function of time as one that is a function of frequency. In this chapter we will introduce the reader to the basics of using MATLAB to work with transforms. In this chapter we will introduce the laplace, fourier, and fft commands