Matlab Basics - WVU Math Department

Matlab Basics I
1
Matlab Basics I (Math 373/578, Spring 2013)
Preparations:
(1) visit
http://www.math.wvu.edu/~hjlai/Math373_Matlab/
and download all the files into a folder (Math373-Matlab).
(2) Open Matlab, change the current directory to this folder. (Click the logo at the right end of
“current Directory” for “Browse for folder”, and change the folder).
(3) Start working on the mathematics.
Ordinary Computations (Review)
Example: Compute
+ 135/5.
>> 2^7 + 135/5
ans =
155
Example: Compute 11(126/(9 + 7) –
>> 11*(126/(9+7) – 2^(72/12))
ans =
-617.3750
Example: Compute
>> 6^2 – sqrt(1024)
ans =
4
.
).
Matlab Basics I
2
Part 1: Number Theory Basics
(1.1) Factor 123456 into primes.
>> factor(123456)
ans =
2
2
(1.2) For
2
2
2
2
3
643
, find gcd(a,b).
>> gcd(a,b)
Example: Find gcd(23456, 987654).
>> gcd(23456,987654)
ans =
2
(1.3) For
, find
such that
.
>> [d,u,v]=gcd(a,b)
Example: Find u, v such that gcd(23456,987654) = 23456u + 987654v.
>> [d,u,v]=gcd(23456, 987654)
d=
2
u=
-3158
v=
75
This means u = –3158 and v = 75. Thus 2 = gcd(23456,987654) =
(1.4) Solve the equation
Example: Solve
when
.
for a = 65337 and b = 3511.
What is the relationship between this problem and the example in (1.3)?
>>[d,u,v]=gcd(65337, 3511)
How do you interpret the answers?
.
Matlab Basics I
3
(1.5) Knowing n, we can factor n and find
by using:
n = 1234567
factor(n)
eulerphi(n)
This can be used for deciphering an RSA coded message in the future. However, Matlab may have
trouble factoring numbers bigger than
.
Matlab Basics I
4
Part 2: Operations involving integers modulo m
(2.1) Given m > 1 and
, find
with
such that
.
mod(a,m)
(2.2) Given m > 1 and
, find
for subtractions and multiplications.)
with
such that
. (Do the same
mod (a+b,m), mod(a–b,m), mod(a*b,m)
(2.3) Find the multiplicative inverse of a (mod m) (assuming that we already know that a and m are
relatively prime).
powermod(a,–1,m)
Example: Find the multiplicative inverse of 8787 (mod 91919).
>> powermod(8787,-1,91919)
ans =
71374
Thus
.
(2.4) Find the multiplicative inverse of a (modm) (assuming we do not know if a and m are relatively
prime).
Example: Determine if 23456 has an inverse mod 987654. If it does, find it.
>> [d,u,v]=gcd(23456,987654)
d =
2
u =
–3158
v =
75
This means that gcd(23456,987654)=2 so the inverse does not exist.
Example: Determine if 23456 has an inverse mod 987651. If it does, find it.
>> [d,u,v]=gcd(23456,987651)
d =
1
Matlab Basics I
5
u =
256892
v =
-6101
This means gcd(23456,987651) = 1 = 23456*(256892) + 987651*(–6101), and so
.
(2.5) Find the modular exponentiation.
Example: Compute
.
>> powermod(234,567,9871)
ans =
5334
So,
.
(2.6) Solving equations.
Example: Solve
of the coefficient of x (if it has one).
. What do we do? We first find the multiplicative inverse
>> gcd(7654,65537)
ans =
1
So 7654 has an inverse mod 65537, and
.
>> powermod(7654,-1,65537)
ans =
54637
>> mod(2389*54637,65537)
ans =
43626
Thus,
.