Matlab code for solving complex numbers

Click For Summary

Discussion Overview

The discussion revolves around solving a complex number equation related to characteristic impedance using MATLAB. Participants share their experiences and challenges with coding, specifically addressing syntax and mathematical operations in MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion over an error message regarding indexing in MATLAB and questions the use of the term "complex" in their code.
  • Another participant clarifies that the intended equation may require additional parentheses to correctly represent the square root of the product of two complex expressions.
  • A third participant emphasizes the need for proper multiplication operators in MATLAB, suggesting the use of element-wise operations.
  • One participant requests to see the original code to better assist with the issue.
  • A later reply provides a working example of MATLAB code that computes values for a range of parameters, explaining the use of element-wise operations and the representation of complex numbers in MATLAB.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper syntax and the use of parentheses in MATLAB, but there is no consensus on the initial formulation of the equation or the best approach to resolve the error.

Contextual Notes

Some limitations include potential misunderstandings of MATLAB syntax, the need for clarification on mathematical operations, and the dependency on specific definitions of complex numbers in the context of the discussion.

Kayne
Messages
22
Reaction score
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
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]
 
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.
 
It would help us out a lot if you posted your code.
 
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
4K