0% found this document useful (0 votes)
30 views4 pages

MATLAB Complex Number Functions

This document provides an introduction to using MATLAB for complex arithmetic and plotting functions. It includes examples of using MATLAB functions like real(), imag(), abs(), angle() to work with complex numbers. It also demonstrates how to define a user function in MATLAB to evaluate a trigonometric function, and how to test the user defined function by plotting it over a time vector.

Uploaded by

fawad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

MATLAB Complex Number Functions

This document provides an introduction to using MATLAB for complex arithmetic and plotting functions. It includes examples of using MATLAB functions like real(), imag(), abs(), angle() to work with complex numbers. It also demonstrates how to define a user function in MATLAB to evaluate a trigonometric function, and how to test the user defined function by plotting it over a time vector.

Uploaded by

fawad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ECE 2610 Lab Worksheet:

MATLAB Intro & Complex Arithmetic


1/24/2011

MATLAB as a Complex Number Calculator


• Functions used: real(), imag(), abs(), angle()
• Compare the three angle producing functions: angle(), atan2(), and atan()

Practice Problems (very similar to Set #1)


For each of the problem below work out the answer using both MATLAB and your calculator
1. Write 127 – j75 in polar form; find the angle in both radians and degrees.
>> z = 127 - j*75;
>> abs(z)
ans = 147.4924
>> angle(z)
ans = -0.5334 % In radians
>> angle(z)*180/pi
ans = -30.5640 % In degrees
Hand/Calculator workspace:
• Since this number lies
From TI89
in quadrant IV, we are
using the numerical
evaluate mode OK using the atan( ) to
(green diamond find the angle
button, them press • Watch out if in quadrant
enter)
II or III
• Best to plot the complex
number as a vector

2. Write z = 22 – 110  in rectangular form.


>> z = 22*exp(-j*110*pi/180)
z = -7.5244 -20.6732i
>> real(z)
ans = -7.5244
>> imag(z)
ans = -20.6732

MATLAB as a Complex Number Calculator 1


ECE 2610 Lab Worksheet: MATLAB Intro & Complex Arithmetic Spring 2011

Hand/Calculator workspace:

Symbolic/algebra
form

Numerical

3. Evaluate z =  15 – j37  – 60 45 to a rectangular form solution.


MATLAB Steps:
>> (15 - j*37) - 60*exp(j*45*pi/180)

ans = -2.7426e+01 - 7.9426e+01i

Hand/Calculator workspace:

4. Evaluate z =  15 – j37   60 45 to a rectangular form solution.


MATLAB Steps:
>> (15 - j*37)/(60*exp(j*45*pi/180))

ans = -2.5927e-01 - 6.1283e-01i

Hand/Calculator workspace:

MATLAB as a Complex Number Calculator 2


ECE 2610 Lab Worksheet: MATLAB Intro & Complex Arithmetic Spring 2011

MATLAB for Plotting Data and Functions


• Functions used: plot(), xlabel(), ylabel(), title(), grid, and axis
1. Plot x  t  = 25 sin  t  5 +   4  for 0  t  15 s. Include a grid and axis labels.
>> t = 0:.1:15; % create a time axis vector with sample spacing 0.1s
>> plot(t,25*sin(pi*t/5+pi/4))
>> grid
>> xlabel('Time (s)')
>> ylabel('Amplitude')
>> print -depsc -tiff wks_fig1.eps









 










   
 

For the x  t  above, plot x  t – 2  for 0  t  15 s, overlaid on the plot of x  t  of part (1).
>> hold on % will hold the previous plot so you can overlay a new plot
>> hold
Current plot held
>> plot(t,25*sin(pi*(t-2)/5+pi/4),'r')
>> print -depsc -tiff wks_fig2.eps



x(t  2)





 



 x(t)





   
 

MATLAB for Plotting Data and Functions 3


ECE 2610 Lab Worksheet: MATLAB Intro & Complex Arithmetic Spring 2011

User Defined Functions in MATLAB


One of the most power capabilities of Matlab is being able write your own user defined functions.
Consider a custom trig function of the form
y  t  = 3 cos  t  + 4 sin  3t  (1)
The input to this function is time, t , and the output is y . The function prototype we require is of
the form:
function y = my_trig(t)
% y = my_trig(t) is a function that evaluates the simple trig
% based function y = 3*cos(t) + 4*sin(3*t).
%
% Author: My Name
% Date: January 2011
%

function body

make sure that you return output to variable y

Write the Function


function y = my_trig(t)
% y = my_trig(t)
% Mark Wickert, January 2011

y = 3*cos(t) + 4*sin(3*t);

Test the Function


To test the function input a time vector that runs from -2s to 10s using a time step of 0.05s. Output
the results in a plot using plot(t,y).
8
>> t = -2:.05:10;
>> y = my_trig(t); 6

>> plot(t,y)
4
>> grid
>> xlabel('Time (s)') 2
Amplitude

>> ylabel('Amplitude')
0
>> print -tiff -depsc
[Link] −2

−4

−6

−8
−2 0 2 4 6 8 10
Time (s)

User Defined Functions in MATLAB 4

Common questions

Powered by AI

MATLAB's plotting capabilities enhance the understanding of mathematical concepts by providing visual representations that clarify abstract or complex ideas. For instance, visual confirmation through plotting can reveal oscillatory behavior in trigonometric functions or convergence patterns in numerical sequences, facilitating better grasp and interpretation. However, challenges may arise in achieving accurate visualizations, such as ensuring proper scaling and labeling, avoiding clutter in overlaid plots, and managing computational limits for high-density plots. Effective use of additional functions like grid and axis labeling is essential to overcome these challenges and maximize educational benefits .

To test a custom trigonometric function in MATLAB, you must first define it using the 'function' syntax. For example, define a function y = my_trig(t) that computes y = 3*cos(t) + 4*sin(3*t). After defining it in a separate function file or within a script, you test it by creating a time vector for the desired range, here from -2s to 10s with a step of 0.05s. Then, call the function with this vector and plot the results using plot(t,y). Adding grid, xlabel, and ylabel helps enhance plot readability. Finally, save the plot using a specified format such as TIFF or EPS .

MATLAB handles complex numbers by providing specific functions to perform operations on them, such as real(), imag(), abs(), and angle(). The real() function extracts the real part of a complex number, imag() extracts the imaginary part, abs() calculates the magnitude, and angle() computes the phase angle in radians. These functions facilitate complex arithmetic and conversion between different forms, as seen in the transformation from rectangular to polar form and vice versa .

The angle() function in MATLAB returns the phase angle in radians of a complex number and considers the sign of both real and imaginary parts to find the correct quadrant, providing a more accurate representation. The atan2() function also determines the angle by looking at the signs of the inputs, enabling it to disambiguate angles in different quadrants, especially useful for quadrant II and III complex numbers. In contrast, atan() only considers the ratio of imaginary to real parts, resulting in potential miscalculations if signs are not explicitly checked. Overall, angle() and atan2() provide more reliable results in quadrant-sensitive calculations than atan().

Plotting functions in MATLAB is crucial for visualizing data and mathematical functions. The significant aspect is its ability to provide graphical representations which aid in understanding trends, behaviors, and relationships within data sets or functions. A common challenge is ensuring plots are accurately labeled, scaled, and interpreted. Overlaying multiple plots requires using the 'hold on' command, which retains the current plot while allowing additional data to be added. This is essential for comparing different datasets directly within the same graph. The steps involve plotting the first function, using 'hold on', and then plotting the second function with different parameters like color or line style for differentiation, as illustrated with overlaying two sine waves .

MATLAB's capability to define and test user functions improves computational efficiency and workflow by encapsulating repetitive calculations within a reusable structure, reducing code redundancy. It enhances modularity, as complex tasks are broken down into simpler sub-tasks, improving code clarity and maintenance. For example, a user-defined function like my_trig(t) can be quickly tested over various inputs using singular function calls, facilitating exploratory data analysis and rapid prototyping. Such features streamline workflow by allowing focus on high-level logic rather than repetitive details, optimizing computational resources .

MATLAB enables user-defined functions through a simple syntax that begins with the 'function' keyword, allowing users to define custom operations or computations that can be reused across scripts. Users specify input and output variables, write the function body, and can document their creation with comments. This ability allows significant flexibility and efficiency in problem-solving as it encapsulates complex computations within a callable entity. Such user-defined functions are beneficial for automating repetitive tasks and enhancing modularity and readability of code, as exemplified by the creation of a custom trigonometric function y = 3*cos(t) + 4*sin(3*t).

To convert a complex number from polar to rectangular form using MATLAB, you start by defining the complex number in polar form using exponential notation. For example, given a magnitude and angle, the number is represented as z = magnitude * exp(j*angle_in_radians). You can then extract the rectangular components using MATLAB functions: real(z) gives the real part, while imag(z) gives the imaginary part. As demonstrated in , z = 22*exp(-j*110*pi/180) yields a rectangular form with real part -7.5244 and imaginary part -20.6732.

A practical application of MATLAB's complex number functions in engineering is in the analysis of electrical circuits, where alternating current (AC) circuit analysis frequently involves complex numbers. Impedances in AC circuits are represented as complex numbers to account for resistive (real part) and reactive (imaginary part) components. Functions like abs(), real(), imag(), and angle() help compute circuit parameters such as magnitude of impedance, phase angles, or conversion between polar and rectangular forms of waveforms, aiding efficient analysis of circuit behaviors under various operational conditions .

The quadrant in which a complex number lies is crucial when calculating its angle because it determines the sign and value of the angle outcome, affecting its correctness. For example, the angle calculation must correctly reflect whether the angle is in quadrant II, III, or IV, impacting whether the phase is positive or negative and potentially adding or subtracting 180 degrees. This consideration influences the choice of functions; while angle() and atan2() automatically handle quadrant considerations, ensuring accurate angle results, atan() does not account for these quadrant discrepancies, making it less reliable unless manual adjustments are made .

You might also like