Complex numbers let us work with numbers that have a real part and an imaginary part. They help solve problems that normal numbers can't handle.
Complex numbers in MATLAB
z = a + b*i;
% or
z = complex(a, b);Here, a is the real part and b is the imaginary part.
In MATLAB, i or j represents the imaginary unit (√-1).
z1 = 3 + 4i;
complex function.z2 = complex(5, -2);
j instead of i for the imaginary unit.z3 = 7 - 3j;
This program creates two complex numbers, adds them, multiplies them, and prints the results showing real and imaginary parts.
a = 2 + 3i; b = complex(1, -1); sum = a + b; product = a * b; fprintf('Sum: %.2f + %.2fi\n', real(sum), imag(sum)); fprintf('Product: %.2f + %.2fi\n', real(product), imag(product));
Use real(z) and imag(z) to get the real and imaginary parts of a complex number.
Be careful not to overwrite i or j as variables, or MATLAB will not recognize them as imaginary units.
Complex arithmetic follows special rules different from real numbers, but MATLAB handles them automatically.
Complex numbers have a real and an imaginary part.
Use i or j to write imaginary parts in MATLAB.
MATLAB provides functions to create and work with complex numbers easily.
Practice
Solution
Step 1: Understand complex number format in MATLAB
Complex numbers are written as real part plus imaginary part times i or j, e.g., 3 + 4i.Step 2: Check each option
3 + 4i uses 3 + 4i which is correct. 3 + 4 misses imaginary part. 3i + 4 swaps real and imaginary incorrectly. 3 + 4j + 1 adds extra 1.Final Answer:
3 + 4i -> Option CQuick Check:
Complex number = 3 + 4i [OK]
- Forgetting to include 'i' or 'j' for imaginary part
- Swapping real and imaginary parts
- Adding extra numbers outside complex format
z with real part 5 and imaginary part -2?Solution
Step 1: Recall MATLAB syntax for complex numbers
Using complex(real, imag) function is a clear way to create complex numbers.Step 2: Analyze options
z = 5 - 2; is just a real number. z = 5 + 2i; creates 5 + 2i (wrong imaginary part). z = 5 + -2; is a real number. z = complex(5, -2); uses complex(5, -2) correctly.Final Answer:
z = complex(5, -2); -> Option AQuick Check:
complex(5, -2) creates correct complex number [OK]
- Writing imaginary part without 'i' or 'j'
- Using wrong sign for imaginary part
- Confusing syntax of complex() function
z1 = 2 + 3i;
z2 = 1 - 4i;
result = z1 * z2;
Solution
Step 1: Multiply complex numbers (a+bi)(c+di)
Use formula: (ac - bd) + (ad + bc)i. Here a=2, b=3, c=1, d=-4.Step 2: Calculate real and imaginary parts
Real: 2*1 - 3*(-4) = 2 + 12 = 14. Imaginary: 2*(-4) + 3*1 = -8 + 3 = -5. So result = 14 - 5i.Final Answer:
14 - 5i -> Option BQuick Check:
(2+3i)*(1-4i) = 14 - 5i [OK]
- Mixing signs in multiplication
- Forgetting to multiply imaginary parts by i^2 = -1
- Adding instead of subtracting real parts
z = 4 + 5k;
disp(z);
Solution
Step 1: Check imaginary unit usage in MATLAB
MATLAB uses 'i' or 'j' for imaginary unit, 'k' is not recognized.Step 2: Analyze code error
Using '5k' causes an undefined variable error. Semicolon is optional. disp() works with complex numbers. Real part can be any number.Final Answer:
Variable 'k' is undefined, should use 'i' or 'j' for imaginary unit -> Option AQuick Check:
Imaginary unit must be 'i' or 'j' [OK]
- Using undefined letters for imaginary unit
- Assuming semicolon is mandatory
- Thinking disp() can't show complex numbers
a = 3 + 4i and b = 2 - 4i. Write MATLAB code to compute the magnitude of their sum and select the correct output.sum_ab = a + b;
mag = abs(sum_ab);
disp(mag);
Solution
Step 1: Calculate sum of complex numbers
a + b = (3 + 2) + (4 - 4)i = 5 + 0i.Step 2: Calculate magnitude using abs()
Magnitude = sqrt(5^2 + 0^2) = sqrt(25) = 5.Step 3: Check output of disp(mag)
MATLAB displays 5.Final Answer:
5 -> Option DQuick Check:
abs(5 + 0i) = 5 [OK]
- Adding magnitudes instead of complex numbers
- Forgetting to use abs() for magnitude
- Calculating magnitude incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
