Matlab code for solving complex numbers

In summary, the conversation was about solving a complex number equation for character impedance using MATLAB. The issue was with the indexing and parentheses in the code, and the correct operators and parentheses were suggested. A sample code was also provided for a range of values.
  • #1
Kayne
24
0
Hi All,

I have been trying to solve a complex number equation for character impedance with MATLAB but it continues to tell me that "indexing must appear last on index addressing". I am new to MATLAB so I think that my code is wrong.

What I am using is

complex sqrt (R+jwL)(G+jwC)

The values for R,w,L,G,C I add in when putting it into matlab.

is complex the right word to be using here?

The equation that I am trying to solve is

[tex]y=sqrt(R+jwL)(G+jwC)[/tex]
 
Physics news on Phys.org
  • #2
Kayne said:
Hi All,

I have been trying to solve a complex number equation for character impedance with MATLAB but it continues to tell me that "indexing must appear last on index addressing". I am new to MATLAB so I think that my code is wrong.

What I am using is

complex sqrt (R+jwL)(G+jwC)
I'm not sure what you mean by the above. As you have it, and using mathematical symbols, it means this:
[tex]\sqrt{R + jwL}(G + jwC)[/tex]

I suspect that you want the square root of the product, which would look like this:
[tex]\sqrt{(R + jwL)(G + jwC)}[/tex]

If that's the case, then using another pair of parentheses might be the fix.
complex sqrt ((R+jwL)(G+jwC))

Kayne said:
The values for R,w,L,G,C I add in when putting it into matlab.

is complex the right word to be using here?

The equation that I am trying to solve is

[tex]y=sqrt(R+jwL)(G+jwC)[/tex]

Might this be the equation you're trying to solve?
[tex]y=\sqrt{(R+jwL)(G+jwC)}[/tex]
 
  • #3
You need to use operators for multiplying. i.e i*w*C (matrix multiplication) or i.*w.*L for elementwise operation.

Also, what Mark said. Use parentheses.
 
  • #4
It would help us out a lot if you posted your code.
 
  • #5
Code:
>> R = 1:5; L = 1:5; G = 1:5; C = 1:5; w = 20;
>> y = (R+1i*w*L).^(1/2).*(G+1i*w*C);
>> y'

ans =

  1.0e+002 *

  -0.5844 - 0.6793i
  -1.6530 - 1.9214i
  -3.0368 - 3.5298i
  -4.6754 - 5.4344i
  -6.5341 - 7.5948i

Here is the operation for a range of R, L, G, and C values giving a range of y values at a specific w. If you just want to use scalars all the way through, remove all the dots. So the first value corresponds to using R(1), L(1), and so on (which is 1 here for all). And y(2) corresponds to using R(2), L(2), and so on (which is 2 here for all).

Note, the .^ applies the power to each element in the matrix (as opposed to doing matrix computations, e.g. ^-1 would find the inverse of the matrix and .^-1 would make each element A_ij => 1/A_ij), and A.*B multiplies each element in A by the same element in B (C_ij = A_ij*B_ij) as opposed to * doing matrix multiplication. And 1i is the preferred representation of the complex i in matlab. Using anything else will give you an optimization warning, even if it works, e.g. i believe using i works too, but it is slower.
 
Last edited:

Related to Matlab code for solving complex numbers

What is Matlab code for solving complex numbers?

Matlab is a programming language that allows for efficient computation and representation of complex numbers. The code for solving complex numbers in Matlab involves using the built-in functions and operators specifically designed for complex numbers, such as real(), imag(), conj(), and abs().

How do I input complex numbers in Matlab?

To input complex numbers in Matlab, use the format a+bi or a+bj, where a represents the real part and b represents the imaginary part. For example, to input the complex number 3+4i, you would write 3+4i in the code.

What is the syntax for complex number operations in Matlab?

The syntax for complex number operations in Matlab is similar to regular arithmetic operations. For example, to add two complex numbers a+bi and c+di, you can use the + operator as (a+c) + (b+d)i. Other operators such as -, *, and / can also be used for subtraction, multiplication, and division respectively.

Can Matlab solve equations involving complex numbers?

Yes, Matlab has built-in functions for solving equations involving complex numbers. These include roots() for finding the roots of a polynomial equation and fzero() for finding the roots of a nonlinear equation. You can also use the solve() function to solve a system of equations involving complex numbers.

Are there any other helpful functions for working with complex numbers in Matlab?

Yes, Matlab has a variety of other functions that can assist with working with complex numbers. These include polar() for converting complex numbers to polar form, cart2pol() for converting Cartesian coordinates to polar coordinates, and pol2cart() for converting polar coordinates to Cartesian coordinates. Additionally, the angle() function can be used to find the angle of a complex number in radians or degrees.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
988
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
930
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top