MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (2024)

[MATLAB Programming\|/MATLAB Programming]m]

Chapter 1: MATLAB ._.

 Instroductions .
Fundamentals of MATLAB
MATLAB Workspace
MATLAB Variables
*.mat files

Chapter 2: MATLAB Concepts

MATLAB operator
Data File I/O

Chapter 3: Variable Manipulation

Numbers and Booleans
Strings
Portable Functions
Complex Numbers

Chapter 4: Vector and matrices

Vector and Matrices
Special Matrices
Operation on Vectors
Operation on Matrices
Sparse Matrices

Chapter 5: Array

Arrays
Introduction to array operations
Vectors and Basic Vector Operations
Mathematics with Vectors and Matrices
Struct Arrays
Cell Arrays

Chapter 6: Graphical Plotting

Basic Graphics Commands
Plot
Polar Plot
Semilogx or Semilogy
Loglog
Bode Plot
Nichols Plot
Nyquist Plot

Chapter 7: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
Debugging M Files

Chapter 8: Advanced Topics

Numerical Manipulation
Advanced File I/O
Object Oriented Programming
Applications and Examples
Toolboxes and Extensions

Chapter 9: Bonus chapters

MATLAB Benefits and Caveats
Alternatives to MATLAB
[MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
Octave/MATLAB differences

edit this box

Contents

  • 1 Complex Numbers
  • 2 Declaring a complex number in MATLAB
    • 2.1 Complex functions
    • 2.2 Arithmetic operations that create complex numbers
  • 3 Manipulate complex numbers
    • 3.1 Finding real and imaginary number
    • 3.2 Complex conjugate
    • 3.3 Phase Angle
  • 4 References

Complex Numbers[edit | edit source]

Complex numbers are also used in MATLAB.
It consists of two parts, one is real number and one is imaginary number. It is in the form of MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (1) or MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (2).
i or j returns the basic imaginary unit. i or j is equivalent to square root of -1 where the formula is ( MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (3) ).
Note: The symbol i and j are interchangeable for one to another, as MATLAB just convert j into i, if equations is using two different notations as shown below.

Declaring a complex number in MATLAB[edit | edit source]

Complex numbers in MATLAB are doubles with a real part and an imaginary part. The imaginary part is declared by using the 'i' or 'j' character. For example, to declare a variable as '1 + i' just type as following:

 >> compnum = 1 + i compnum = 1.000 + 1.000i>>%Note:If you use j MATLAB still displays i on the screen >> compnum = 1 + j compnum = 1.000 + 1.000i

Note 1: Even if you use j to indicate complex number , MATLAB will still displays i on the screen.

Note 2: Since i is used as the complex number indicator it is not recommended to use it as a variable, since it will assume i is a variable.

 >> i = 1; %bad practise to use i as variable >> a = 1 + i a = 2

However, since implicit multiplication is not normally allowed in MATLAB, it is still possible to declare a complex number like this:

 >> i = 3; >> a = 1i + 1 a = 1.000 + 1.000i

It's best still not to declare i as a variable, but if you already have a complex program with i as a variable and need to use complex numbers this is probably the best way to get around it.

If you want to do arithmetic operations with complex numbers make sure you put the whole number in parenthesis, or else it likely will not give the intended results.

Complex functions[edit | edit source]

However, the best practice to declare a complex number is by using function complex.

>>%Best practise to declare complex number in MATLAB>> complex(2,6)ans = 2.0000 + 6.0000i

If you want to declare just the imaginary number, just use the square root of negative numbers, such as followed.

>> sqrt(-49)ans = 0.0000 + 7.0000i

To declare multiple complex numbers , create two row vectors with real numbers and another with imaginary numbers. Combine both of them using complex functions

>> %create a vector consiots of real number>> RE = [1,2,3]RE = 1 2 3>> %create a vector consiots of imaginary number>> IM = [4,5,6]IM = 4 5 6>> %create 3 complex number >> complex(RE,IM)ans = 1.0000 + 4.0000i 2.0000 + 5.0000i 3.0000 + 6.0000i

Arithmetic operations that create complex numbers[edit | edit source]

There are several operations that create complex numbers in MATLAB. One of them is taking an even root of a negative number, by definition.

>> (-1)^0.5ans = 0.000 + 1.000i>> (-3)^0.25ans = 0.9306 + 0.9306i

As a consequence of the Euler formula, taking the logarithm of a negative number also results in imaginary answers.

>> log(-1)ans = 0 + 3.1416i

In addition, the roots of functions found with the 'roots' function (for polynomials) or some other rootfinding function will often return complex answers.

Manipulate complex numbers[edit | edit source]

Finding real and imaginary number[edit | edit source]

First of all, it is helpful to tell whether a given matrix is real or complex when programming, since certain operations can only be done on real numbers.

Since complex numbers don't have their own class, MATLAB comes with another function called "isreal" to determine if a given matrix is real or not. It returns 0 if any of the inputs are complex.

>> A=[complex(2,3),complex(4,0)]A = 2.0000 + 3.0000i 4.0000 + 0.0000i>> %returns 1 if complex number does not have an imaginary part and 0 if otherwise.>> %A denotes the whole vectors>> isreal(A)ans = logical 0>> %A(2) denotes second complex number in the vector (4+0i)>> isreal(A(2))ans = logical 1

Notice that it is possible to have real and complex numbers in the same array, since both are of class double.
The function is set up this way so that you can use this as part of a conditional, so that a block only is executed if all elements of array A are real.

To extract just the real part of a complex variable use the real function. To extract just the complex part use the imag function.

>>%Extract real number from the complex number vector A>> real(A)ans = 2 4>>%Extract imaginary number from the complex number vector A>> imag(A)ans = 3 0

Complex conjugate[edit | edit source]

To find complex conjugate , we can use conj function. If complex number, Z is MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (5) , then the conjugate, Ẑ is MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (6)

>> conj(A)ans = 2.0000 - 3.0000i 4.0000 + 0.0000i

Phase Angle[edit | edit source]

To find phase angle , we can use the phase angle in the radian for each element of a complex numbers

>> angle(A)ans = 0.9828 0

References[edit | edit source]

[1]

  1. https://web.archive.org/web/20211006102547/https://www.maths.unsw.edu.au/sites/default/files/MatlabSelfPaced/lesson1/MatlabLesson1_Complex.html
MATLAB Programming/Complex Numbers - Wikibooks, open books for an open world (2024)
Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6337

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.