Thread Subject: keep variable positive?

Subject: keep variable positive?

From: Sherwin

Date: 6 May, 2009 16:42:01

Message: 1 of 11

Hi,

I'm trying to develop a Simscape model of a simple pneumatic cylinder, assuming perfect heat transfer and ideal air as the gas. And I keep running into problems with the volume going negative, which I know is a result of the math. Does anyone know of a way to make sure a variable remains positive, that is the minimum value of the variable is zero?

The volume is defined as an initial value using the ideal gas law (PV = nRT) and further defined in the equations setting as:

Vol.der == velocity * piston_area


Any help is greatly appreciated!

Subject: keep variable positive?

From: Kaushik

Date: 6 May, 2009 17:00:18

Message: 2 of 11

"Sherwin " <syeo@sustainx.com> wrote in message <gtsekp$dr$1@fred.mathworks.com>...
> Hi,
>
> I'm trying to develop a Simscape model of a simple pneumatic cylinder, assuming perfect heat transfer and ideal air as the gas. And I keep running into problems with the volume going negative, which I know is a result of the math. Does anyone know of a way to make sure a variable remains positive, that is the minimum value of the variable is zero?
>
> The volume is defined as an initial value using the ideal gas law (PV = nRT) and further defined in the equations setting as:
>
> Vol.der == velocity * piston_area
>
>
> Any help is greatly appreciated!

Doesn't uint32/uint16/uint8 help? Say I had var = -67. If I do var = uint8(var), I'll get var = 0.
I'm not sure how much this'll help though.

Kaushik.

Subject: keep variable positive?

From: Matt Fig

Date: 6 May, 2009 17:14:04

Message: 3 of 11

If it was my model which kept giving a negative volume, I would double check the model. Something doesn't sound right.

Subject: keep variable positive?

From: Sherwin

Date: 6 May, 2009 17:41:02

Message: 4 of 11

"Matt Fig" <spamanon@yahoo.com> wrote in message <gtsggs$agr$1@fred.mathworks.com>...
> If it was my model which kept giving a negative volume, I would double check the model. Something doesn't sound right.


Haha, yeah, something isn't right and I'm just trying to explore the different options that will hopefully make it right.

Subject: keep variable positive?

From: Brian

Date: 6 May, 2009 19:20:17

Message: 5 of 11

one trick I've sometimes used - can you formulate the model using the variable squared? then (assuming it doesn't take on complex values) the variable itself will stay non-negative

Subject: keep variable positive?

From: Sherwin

Date: 6 May, 2009 20:24:01

Message: 6 of 11

Brian <briantracey@rcn.com> wrote in message <17169939.65782.1241637647850.JavaMail.jakarta@nitrogen.mathforum.org>...
> one trick I've sometimes used - can you formulate the model using the variable squared? then (assuming it doesn't take on complex values) the variable itself will stay non-negative

The problem with this and the uint command method is the behavior is not supposed to suddenly turn the other way but keep zero until it would naturally change back.

Subject: keep variable positive?

From: Kaushik

Date: 8 May, 2009 14:02:03

Message: 7 of 11

"Sherwin " <syeo@sustainx.com> wrote in message <gtsrl1$9qg$1@fred.mathworks.com>...
> Brian <briantracey@rcn.com> wrote in message <17169939.65782.1241637647850.JavaMail.jakarta@nitrogen.mathforum.org>...
> > one trick I've sometimes used - can you formulate the model using the variable squared? then (assuming it doesn't take on complex values) the variable itself will stay non-negative
>
> The problem with this and the uint command method is the behavior is not supposed to suddenly turn the other way but keep zero until it would naturally change back.

Ok. If you want it to be *absolutely* bounded as 'variable' >= 0, then use the line :

variable=max(0,expression);

in place of every statement in which you modify 'variable' as "variable=expression". This way, the value held by 'variable' cannot go below 0, no matter what.

- Kaushik.

Subject: keep variable positive?

From: Sherwin

Date: 8 May, 2009 14:15:03

Message: 8 of 11

"Kaushik " <kaushikkannan.13091989@yahoo.co.in> wrote in message <gu1e0r$kph$1@fred.mathworks.com>...
> "Sherwin " <syeo@sustainx.com> wrote in message <gtsrl1$9qg$1@fred.mathworks.com>...
> > Brian <briantracey@rcn.com> wrote in message <17169939.65782.1241637647850.JavaMail.jakarta@nitrogen.mathforum.org>...
> > > one trick I've sometimes used - can you formulate the model using the variable squared? then (assuming it doesn't take on complex values) the variable itself will stay non-negative
> >
> > The problem with this and the uint command method is the behavior is not supposed to suddenly turn the other way but keep zero until it would naturally change back.
>
> Ok. If you want it to be *absolutely* bounded as 'variable' >= 0, then use the line :
>
> variable=max(0,expression);
>
> in place of every statement in which you modify 'variable' as "variable=expression". This way, the value held by 'variable' cannot go below 0, no matter what.
>


Thanks!

This worked out very well.

Sherwin
> - Kaushik.

Subject: keep variable positive?

From: Robert

Date: 12 Jun, 2009 11:43:01

Message: 9 of 11

I can't use the max() function in the equations section of Simscape. Were you using it somewhere else. I am trying to constrain an across variable that I want to ensure only the positive solution is used. Example

Equations
y==x^2;

I want to restrict solutions to x >=0. I realize I could explicitly solve for x in this case, but is there a way to constrain the variable for equations where there is not an explicit form?

Thanks,
Rob

Subject: keep variable positive?

From: Arnaud Miege

Date: 15 Jun, 2009 12:27:01

Message: 10 of 11

"Robert " <rjseme@rit.edu> wrote in message <h0tf05$cns$1@fred.mathworks.com>...
> I can't use the max() function in the equations section of Simscape. Were you using it somewhere else. I am trying to constrain an across variable that I want to ensure only the positive solution is used. Example
>
> Equations
> y==x^2;
>
> I want to restrict solutions to x >=0. I realize I could explicitly solve for x in this case, but is there a way to constrain the variable for equations where there is not an explicit form?
>
> Thanks,
> Rob

Hi Rob,

As you pointed out, max is not supported in the equation section, so you would need to do it manually using if else statetments. Here is an example of mine, where I am trying to determine the max of 2 temperatures:

dT1 == WA1.T - WB1.T;
dT2 == WA2.T - WB2.T;
if abs(dT1)>abs(dT2)
   dTmax == abs(dT1);
else
   dTmax == abs(dT2);
end

where the variables have been defined as follows:
 dT1 = { 0 , 'K'};
 dT2 = { 0 , 'K'};
 dTmax = { 0 , 'K'};

HTH,

Arnaud

Subject: keep variable positive?

From: Robert

Date: 15 Jun, 2009 19:43:01

Message: 11 of 11

Arnaud,

Thanks, I will give this a try.
Rob

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
max Arnaud Miege 15 Jun, 2009 08:29:04
simscape language Arnaud Miege 15 Jun, 2009 08:29:04
positive Sherwin 6 May, 2009 12:44:24
variables Sherwin 6 May, 2009 12:44:24
simscape Sherwin 6 May, 2009 12:44:24
rssFeed for this Thread

Contact us at files@mathworks.com