Skip to main content
1-Visitor
October 1, 2015
Solved

Plots for Conditional Function

  • October 1, 2015
  • 7 replies
  • 19814 views

Hi

I am a beginner in Mathcad Prime. I would like to make plot for under stated conditional function .Please help in this regard.

V(t)= 5  for 0<t<5

     =-5  for5<t<10

It should be square wave plot with period 10. I mean after t=10 it should repeat itself.

Best answer by StuartBruff

SAMEER GARG wrote:

Please find final outcome as per your suggestion

fff.png

Are you sure about your equations?  I'm not sure they will give you the minimized value you are possibly seeking.

A few comments.

You have defined I1 and Vripple_min to be functions; this means you cannot put an evaluation operator (=) after them - this is in part what Mathcad is complaining about.

It's usually well worth while evaluating functions at a few values (especially critical ones) and/or plotting your functions to check if they make sense over the intervals of interest.

I'm surprised Mathcad let you type in KHz instead of kHz; the SI multiplier for 1000 (kilo) always has abbreviation 'k' not 'K'.  Indeed, 'K' is often used in computing circles to mean 2^10 = 1024 (eg, KB).

It helps others if you post your worksheet - saves all that typing and there are sometimes problems with a worksheet that aren't obvious or visible from a screenshot.

Stuart

7 replies

23-Emerald IV
October 1, 2015

Success!
Luc

sgarg1-VisitorAuthor
1-Visitor
October 2, 2015

Hi

Thanks for your response. I have been through your way but i am not able to plot the attached expression. Can you please tell where i am doing mistake?I am expecting a periodic square waveform toggling between +/-24V with on and off  period 5us.

Here I used Ton,Toff as a cons; x as a variable and Vlp(x) as a function

Conditional function.png

Conditional function.png

Regards

Sameer

23-Emerald IV
October 5, 2015

Sameer,

The error message points into the direction of something undefined (NaN stands for Not A Number).

You are plotting Vlp versus x, but haven't defined x. That means it (probably) defaults to the range -10 to 10. The first problem you definition of Vlp then poses is what to output for x=-10 (or any other negative of x). So you have to fix that. Either by defining x to have only positive values, or (better?) by including output for negative values of x in the definition of Vlp.

Success!
Luc

25-Diamond I
October 1, 2015

Unfortunately using mod() to make the function periodic will fail for negative arguments. This is because the mod() function is implemented in such a way that it will return negative values for negative arguments -> mod(-12,5)=-2 (and not 3).

You can cope with that problem by either overwrite the built-in mod() function with one of your own or by applying mod() twice (after adding the period T after the first mod().

Here is a different way to define a piecewise function using the programming if statement instead of the if function. If you are using Prime Express or intend to create worksheets for Express, you have to use nested if-functions as programming is a premium feature not available in Express.

Furthermore I show three different ways to make a function periodic. The last one, using recursion, is in no way recommended and is only there as for academic interest.

It was created in Mathcad 15 but I see no reason why it shouldn't work the same way in Prime, too.

23-Emerald V
October 2, 2015

Werner Exinger wrote:

Unfortunately using mod() to make the function periodic will fail for negative arguments. This is because the mod() function is implemented in such a way that it will return negative values for negative arguments -> mod(-12,5)=-2 (and not 3).

Furthermore I show three different ways to make a function periodic. The last one, using recursion, is in no way recommended and is only there as for academic interest.

It was created in Mathcad 15 but I see no reason why it shouldn't work the same way in Prime, too.

True.  Why they implemented trunc mod instead of floor or, preferably, Euclidean mod, is beyond me.

...

Furthermore I show three different ways to make a function periodic. The last one, using recursion, is in no way recommended and is only there as for academic interest.

Oy, oy, oy.  What is it it with you and recursion, already,  Werner?  Did it steal your hubcaps or forget to feed the cat? Or does it still owe you 10 euros?

Recursion is generally fine for many applications and its performance isn't too far off it its iterative counterparts.  If an algorithm is easier to implement or makes more sense (to me) done recursively, then there's usually no significant penalty. It's generally only with poorly designed recursive algorithms, deep recursion and Mathcad's hard-wired recursion depth that problems will occur.  Indeed,  one of the problems I have with Prime is that it doesn't allow local recursive functions, and that means I've either got to rewrite them in iterative form or have a superfluous external recursive function, and it's usually just not worth the effort of rewriting them.

With your definition of f3 and the number of cycles, it will hardly present a problem. It will probably do around 800-900 cycles before you start to notice any performance hit.

Stuart

25-Diamond I
October 2, 2015

Maybe recursion owes me more than just 10 Euro in the meantime 😉

Honestly I like playing around with recursion but when it comes to permanent implementation I'd usually prefer an alternative for performance and stability reasons.

But I agree that in this case I, too, don't see much reason not to use the recursive approach other than the small performance advantage of my second version.

I am surprised that the non-periodic f_0 is performing that bad when you did the timing. I tried, too, and f_0 would outperform all the others.

I also introduce a new fourth variation which simulates what the recursive variant does using a while-loop but the gain in speed was by no means noteworthy. I would have thought that getting rid of multiple function calls, bulding up recursion stack would have a bigger effect.

WE

25-Diamond I
October 1, 2015

The question seems to come up quite often.

I just found a Prime 3 sheet which I posted some time ago as an answer to someones question.

There I turned the first two methods to make a function periodic into convenient functions. Maybe its of help to you, too. The meaning of the arguments should be obvious.

EDIT: And now I also found the thread the file belongs to: Re: Repeating periodic function

1-Visitor
October 2, 2015

sign of the sine is what I think you want.

25-Diamond I
October 2, 2015

I am confused and maybe you can help me.

Is there anything new in your file which LucMeekes did not show a day ago?

BTW, I considered the question to be of a more general nature, the square wave being just an example.

WE

sgarg1-VisitorAuthor
1-Visitor
October 2, 2015

Thanks for your response but as i told i am beginner in Mathcad Prime so I was looking for some basic format. Sorry but i scared by seeing the solution in terms of Mod(), floor() etc. After googling i got may be understated expression can solve my problem for the time being

Conditional function.png

23-Emerald V
October 5, 2015

SAMEER GARG wrote:

Thanks for your response but as i told i am beginner in Mathcad Prime so I was looking for some basic format. Sorry but i scared by seeing the solution in terms of Mod(), floor() etc. After googling i got may be understated expression can solve my problem for the time being

E

Yes, it will.  Provided you don't want to use more than a 100 or so cycles.  After that you will find your calculations start to take longer as you have, in many people's estimations, strayed into a far scarier territory than mod and floor ... Recursion!

For every cycle (multiple of 10) beyond the first one, the function V calls itself.   This means the software will save its current working environment (eg, list of variables) in memory so that it can evaluate the "new" version of V.  After calculating the final V, Mathcad then has to restore all of the saved environments in turn until it gets back to the normal working level.   All of this takes time. 

It's better to get to grips with understanding mod, floor, ceil, trunc and the other rounding functions.  You will find they can make life considerably easier when doing numerical work.

Stuart

sgarg1-VisitorAuthor
1-Visitor
October 19, 2015

Hello Everyone

Thanks for your kind response. I would like to calculate max and minima of  any function. I was going through Max and min function but i was getting some unpredictable answer. Please help in this regard.

Note: It is a part of system so all variables are defined previously.

agfarg.png

23-Emerald I
October 19, 2015

Try

"max(Vripple(t))"

23-Emerald V
October 19, 2015

Fred Kohlhepp wrote:

Try

"max(Vripple(t))"

I'm not sure that will give the desired result - t is a range variable, so max should return the "maximum" (ie, the only) for each value of t.

Stuart

sgarg1-VisitorAuthor
1-Visitor
October 20, 2015

Hi to all

Thanks for your kind interest. But sorry to say I am not getting any solution yet. Please find attached DOC for the reference.I actually want to find critical point of any function. Maximize/Minimize are suppose to put on Vector but I am not getting what makes not to it to do so.Problem.jpg

23-Emerald I
October 20, 2015

It looks like the t in the argument for I(t) is considered to be a unit.  (Prime makes units bold and dark blue.)  In the math tab on the ribbon, pull down "labels" and make that "t" a variable.  That should get rid of the red box in that equation and in the definition of V ripple.  Then all you have to worry about is the correct set up for minimize.  (I think it takes a guess and/or a range.)

sgarg1-VisitorAuthor
1-Visitor
October 20, 2015

Hello Fred Kohlhepp

I did the same but it not works out.