cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

18-Opal

## Discrepancy in Bode Plot: Phase - Frequency Plot -> Mathcad vs Matlab

Hello Dear Friends,

How can it be solved that the phase - frequency graph of the bode diagram in Mathcad looks like the phase - frequency graph of the bode diagram in Matlab?

Attached is the Mathcad 15 file with the details.

Thank you.
Best regards.

1 ACCEPTED SOLUTION

Accepted Solutions
24-Ruby V
(To:Cornel)

Matlab's bode somehow finds a way to examine the transfer function and so to determine the best range the phase should be placed at.

Mathcads arg function doesn't know anything about the transfer function - its simply receives a complex number and returns its argument, the angle, and this will always be in the range (-pi; +pi]. This range is as good as any other and its similar to the atan2 function. And thats also the reason you get the jump up to +180° step when the angle would go below -180°. "arg" would never give you an angle below (or equal to) -180°.

Easiest solution is to write an alternative "arg" function which returns the angle in a different 2 pi interval.

Put it at the top of the sheet (maybe in a collapsed region).

Or you may use an all-purpose "Arg" function where you can choose the start of the 2pi interval as its second argument:

The drawback of these simple solutions are, that you have to determine and chose the range for the angle manually yourself and that they only work OK as long as the range of angle values does not exceed a 2 pi interval.

49 REPLIES 49
24-Ruby V
(To:Cornel)

Matlab's bode somehow finds a way to examine the transfer function and so to determine the best range the phase should be placed at.

Mathcads arg function doesn't know anything about the transfer function - its simply receives a complex number and returns its argument, the angle, and this will always be in the range (-pi; +pi]. This range is as good as any other and its similar to the atan2 function. And thats also the reason you get the jump up to +180° step when the angle would go below -180°. "arg" would never give you an angle below (or equal to) -180°.

Easiest solution is to write an alternative "arg" function which returns the angle in a different 2 pi interval.

Put it at the top of the sheet (maybe in a collapsed region).

Or you may use an all-purpose "Arg" function where you can choose the start of the 2pi interval as its second argument:

The drawback of these simple solutions are, that you have to determine and chose the range for the angle manually yourself and that they only work OK as long as the range of angle values does not exceed a 2 pi interval.

18-Opal
(To:Werner_E)

Ok.

Thank you.
Best regards.

24-Ruby V
(To:Cornel)

I just noticed that most of what i said above was already covered in a thread of yours with a similar question

In this detailed discussion I provided a "bode" function which you could have used - why didn't you?

Furthermore later in the very same discussion the "phasecor" function was brought to my/our attention and it could also help with the problem of discontinuities in bode plots:

20-Turquoise
(To:Werner_E)

18-Opal
(To:Werner_E)

Is there any way to result in the corresponding value of the phase margin as shown in the graph, but using the above functions (or others, is not a problem), and not looking at the graph and reading from it???

Bode Diagram: Magnitude - Frequency Plot:

Attached is the Mathcad 15 file with the details.

Thank you.
Best regards.

24-Ruby V
(To:Cornel)

Did you notice that you can also plot the magnitude using the results of "bode". You may consider modifying "bode" to do the log and times 20 itself.

I can image that you are looking for something like this:

24-Ruby V
(To:Werner_E)

Not able to edit my previous post.

Here is the MC15 file attached.

I also added a function to do the job - may come handy.

I wasn't sure about the adding of 180° which you did, so I did without it. You may add it in the function if thats more appropriate.

18-Opal
(To:Werner_E)

"I wasn't sure about the adding of 180° which you did, so I did without it. You may add it in the function if thats more appropriate."

Is used in control engineering for system stability.

https://www.electrical4u.com/bode-plot-gain-margin-phase-margin/

### Phase Margin Formula:The formula for Phase Margin (PM) can be expressed as:

Where  is the phase lag (a number less than 0). This is the phase as read from the vertical axis of the phase plot at the gain crossover frequency.

24-Ruby V
(To:Cornel)

So I guess you should add the 180° already in the function, in case you decide to use it.

18-Opal
(To:Werner_E)

Ok.

If I encounter any more "adventures" in the future I will post.

Until then... thanks for suggestions, answers, and time.

Thank you.
Best regards.

18-Opal
(To:Werner_E)

@Werner_E
First of all, thank you for your support in creating those functions, your time, and your suggestions.

I came up with a new idea/challenge (as I said in the previous message, I'm coming back :))) to which I still can't find an answer/solution.
Maybe you have a solution/idea.

I would like to find out using a function, something, from the bode diagram of the phase - frequency graph, the frequency at which the graph cuts the value of -180 degrees, and not looking at the graph and reading from it.

I can't find the right option/variant.

Could you take a look?

Than you.
Best regards.

18-Opal
(To:Cornel)

And then to find gain margin (GM) as in the chart above, from a previous comment.

This is the final idea I want to reach, to find an answer.

23-Emerald III
(To:Cornel)

To find the phase crossover, how about:

Instead of finding the phase 180°, turn the phase function and search for 0°.

And of course, to find the 0 dB in the gain, search for gain=1:

Success!

Luc

18-Opal
(To:LucMeekes)

Thank you.

23-Emerald III
(To:Cornel)

So your margins can be easily calculated, without a phase correction function:

(Note: this was on the function:

with

)

Success!
Luc

23-Emerald III
(To:LucMeekes)

Of course this must be corrected (function parameter is H, not Tc, so function should use H, not Tc):

Sorry for any inconvenience caused.

Luc

18-Opal
(To:LucMeekes)

@Werner_E@LucMeekes@Fred_Kohlhepp@ttokoro@-MFra-

As far as I know, so far, I think the next option seems to be good (inspired by @LucMeekes  suggestion:))):

For testing let's take the following example:
The Plant Transfer Function is:

The fact that the function does not return a value for:

is due to the fact that in the phase-frequency graph from the bode characteristic, this graph does not cut the -180 degree axis, which translates into:

Which sounds good, that is, okay with what it looks like on the graphs.

Now if you have any idea if it can be realized that when the phase-frequency characteristic from the bode diagram does not cut the -180 degree axis, that the three values ​​directly return infinite values, and not be red as shown above?

Even so, if you can't, it's fine, very good because I know what that means now. :))

Matlab verification:

Thank you.
Best regards.

24-Ruby V
(To:Cornel)

Hmmm ...

23-Emerald I
(To:Cornel)

One feeble attempt (Prime 4)

I can't open the Mathcad file (Prime 4).
Can you put a picture, something?

23-Emerald I
(To:Cornel)

Can you put a picture, something?

Phase margin plot

Gain Margin plot

24-Ruby V
(To:Cornel)

Test_1: "phasecor" can only be applied to vector arguments, not functions. So Test_1 has to fail

Test_3: "root" will only work on continuous functions, not on vectors like B<2>

Test_2: "root" will only work on continuous functions. Unfortunately arg(...) has a discontinuity, a jump, at +/- pi and so the function has to fail.

You may use a modified arg-function as described elsewhere in this thread which creates a continuous function at -180°. Using this function your Test_2 approach may work. The drawback is that using a modified arg-function requires you to decide on how to modify the arg function dependent on the specific transfer function and the range of angles you'd like to see.

But you could use the second "flavor" of the root function, where you don't have to provide a range (10^1 to 10^7 in your example) but a reasonable guess value:

Note that root finds the zeros of a function and to solve the equation f(x)=-180 you have to look for the zeros of the function f(x)+180

Another way I could think of is to do it similarly as I had shown it for the frequency where the magnitude equals 1. That means creating a large vector of frequency values and corresponding phase values which are "corrected" using phasecor. Then look in the phase vector for a phase near -180!

18-Opal
(To:Werner_E)

That turned out well.
Excellent.

Now I'm struggling with this function because I'm trying to do code-like with the function above.

It doesn't go well :((.

Why?

What am I wrong or omitted?

24-Ruby V
(To:Cornel)

Why?

What am I wrong or omitted?

??? The values you mark as "Not good" seem to be the same as the ones you mark as "Good value", just shown in radiant and not in degrees.

18-Opal
(To:Werner_E)

Good!

I found a special case for a system (or rather, for several systems, as if its power increases, for example: s ^ 2, s ^ ​​3, etc.), I receive the same message.

How could this be remedied?

Thank you.
Best regards.

24-Ruby V
(To:Cornel)

> How could this be remedied?

I don't known

maybe there is no way to deal automatically with all possibly eventualities in Mathcad, maybe there is and we have not yet found it 😉

18-Opal
(To:Werner_E)

@Werner_E@Fred_Kohlhepp@LucMeekes

In any case, I would like to thank you very, very, very much so far for your ideas, suggestions, and for your time and involvement. These suggestions were very useful to me in the turning points.
if in the future you will find new ideas and suggestions about what has been discussed so far here, this is the good topic to post :).
Likewise, if I find any more "doubts"/more "questions", I will post them as well.

Thank you very much.
Best regards.

23-Emerald III
(To:Cornel)

With 1/s you have a first-order function. It'll not come near instability.

You may want to use symbolic functions to assess the order of the H function (or of its numerator and denominator) to find if you need to check for instability at all.

Success!
Luc

23-Emerald I
(To:Cornel)

If you calculate and plot the gain of 1/s, you get

Since the gain NEVER gets to 0 dB, so there is no root, and you can't find that frequency.

Announcements
Top Tags