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

[MATLAB Programming\|/MATLAB Programming]m]

Chapter 1: MATLAB ._.

 Introductions .
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 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)

FAQs

Can MATLAB work with complex numbers? ›

In MATLAB®, i and j represent the basic imaginary unit. You can use them to create complex numbers such as 2i+5 . You can also determine the real and imaginary parts of complex numbers and compute other common values such as phase and angle.

How to draw complex numbers in MATLAB? ›

In Matlab complex numbers can be created using x = 3 - 2i or x = complex(3, -2). The real part of a complex number is obtained by real(x) and the imaginary part by imag(x). The complex plane has a real axis (in place of the x-axis) and an imaginary axis (in place of the y-axis).

How to initialize complex numbers in MATLAB? ›

z = complex( a , b ) creates a complex output, z , from two real inputs, such that z = a + bi .

What is an example of a complex code in MATLAB? ›

For example: x = 5 + 6i; % x is a complex number by assignment. y = complex(5,6); % y is the complex number 5 + 6i. After assignment, you cannot change the complexity of a variable.

How slow is MATLAB compared to C++? ›

However, one of the journal reviewrs is saying that the scripts based on Fortran and C++ language are usually running faster than any other computational languages, and according to the reference [1] shown as follows, the MATLAB is usually 9 to 11 times slower than the best C++ executable.

What are the limitations of MATLAB? ›

  • Limitations.
  • Sample Time and Solver Restrictions.
  • Algebraic Loops.
  • Unsupported Simulink Tools and Features.
  • Restricted Simulink Tools.
  • Simulink Tools Not Compatible with Simscape Blocks.
  • Code Generation. Code Generation and Fixed-Step Solvers.

How do you simplify complex numbers in MATLAB? ›

In most cases, to simplify a symbolic expression using Symbolic Math Toolbox™, you only need to use the simplify function. But for some large and complex expressions, you can obtain a faster and simpler result by using the expand function before applying simplify .

How to generate 10000 random numbers in MATLAB? ›

Generate random numbers from the distribution. rng('default') % For reproducibility r = random(pd,10000,1);

How to generate random complex numbers in MATLAB? ›

X = randn(___,"like", p ) returns an array of random numbers like p ; that is, of the same data type and complexity (real or complex) as p . You can specify either typename or "like" , but not both. X = randn( s ,___) generates numbers from random number stream s instead of the default global stream.

How do you write the real part of a complex number in MATLAB? ›

X = real( Z ) returns the real part of each element in array Z .

What is the pi in MATLAB? ›

The pi in matlab is not a real 'pi', but it is only a floating-point number close to 'pi'. Trigonometric functions around pi may have errors close to machine precision. Note that sin(pi) returns '1.22464679914735e-16' and it is true somehow.

What are the different forms of complex numbers? ›

Complex numbers have three primary forms: the general form, z=a+ib; the polar form, z=r(cosθ+isinθ); and the exponential form, z=rexp(iθ).

How do you handle complex numbers in MATLAB? ›

You can use i to enter complex numbers. You also can use the character j as the imaginary unit. To create a complex number without using i and j , use the complex function. z = a + b i returns a complex numerical constant, z .

Can you plot complex numbers in MATLAB? ›

You can plot a complex number as a pair of coordinates ( x , y ) on the complex plane, also known as the Argand diagram.

How to write a complex equation in MATLAB? ›

Direct link to this answer
  1. z = 1 + 3*1i ; % make a complex number.
  2. R = real(z) % Extract real part of Z.
  3. I = imag(z) % Extract imaginary part of Z.
  4. A = abs(z) ; % GEt absolute of complex number.
Dec 3, 2017

How to do complex integration in MATLAB? ›

In MATLAB®, you use the 'Waypoints' option to define a sequence of straight line paths from the first limit of integration to the first waypoint, from the first waypoint to the second, and so forth, and finally from the last waypoint to the second limit of integration.

Which programming language supports complex numbers? ›

The C programming language, as of C99, supports complex number math with the three built-in types double _Complex, float _Complex, and long double _Complex (see _Complex). When the header <complex. h> is included, the three complex number types are also accessible as double complex, float complex, long double complex.

How do you sort complex numbers in MATLAB? ›

B = cplxpair(A) sorts the elements along different dimensions of a complex array, grouping together complex conjugate pairs. The conjugate pairs are ordered by increasing real part. Within a pair, the element with negative imaginary part comes first. The purely real values are returned following all the complex pairs.

Top Articles
Kittens for sale in London | Pets4Homes
Sam's Club Gas Price Mechanicsburg Pa
Salons Open Near Me Today
What to Do For Dog Upset Stomach
104 Whiley Road Lancaster Ohio
Blowupgirls Thread
Ascension St. Vincent's Lung Institute - Riverside
Busted Mugshots Rappahannock Regional Jail
Busted Newspaper Birmingham Al
Umc Webmail
Spur H0 » Details Trix H0 Profi Club Modell 2009
Fire And Ice Festival Dc
When Does Dtlr Close
Myvetstoreonline.pharmacy
Lsn Nashville Tn
Culver's Flavor Of The Day Paducah Ky
How Nora Fatehi Became A Dancing Sensation In Bollywood 
303-615-0055
Cuộc thi “Chung tay vì an toàn giao thông” năm 2024
Zipcar Miami Airport
159 Joseph St, East Brunswick Township, NJ 08816 - MLS 2503534R - Coldwell Banker
2024 Coachella Predictions
NFL Week 1 coverage map: Full TV schedule for CBS, Fox regional broadcasts | Sporting News
Papa's Games Unblocked Games
Craigslist Folding Table
Managing Your Activision Account
Evil Dead Rise Showtimes Near Cinemark Movies 10
Watch The Most Popular Video Of Mikayla Campinos Online
Hdtoday.comtv
Watch The Lovely Bones Online Free 123Movies
Espn Masters Leaderboard
Карта слов и выражений английского языка
Importing Songs into Clone Hero: A Comprehensive Tutorial
Kagtwt
Crimson Draughts.
Best Upscale Restaurants In Denver
Aspect of the Dragons
Mula Pelada
Cavender's Boot City Lafayette Photos
Längen umrechnen • m in mm, km in cm
Sherwin Williams Buttercream
Edenmodelsva
Craigslist Tools Las Cruces Nm
Grayson County Craigslist
When is the next full moon? September's Harvest Moon is also super
Top 10 websites to play unblocked games
Motorcycle Sale By Owner
Lesson 2 Homework 4.1 Answer Key
Accident On 40 East Today
Saratoga Otb Results
Only Partly Forgotten Wotlk
Twisted Bow Osrs Ge Tracker
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5817

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.