# IF statement on LTspice

Status
Not open for further replies.

#### Firenze02

##### New Member
Hi,

I would like to create a variable resistor as following:

R2=1 if i(R2)<1
R2=2 if 1<i(R2)<10
R2=3 if i(R2)>10

I know that the sintax is:

R=if(condition,valueiftrue,valueiffalse)

What about if I have three intervals?

The .asc file and the IF statement law is also shown in the attached files.

Which is the correct sintax to do that?

Thank you.

#### Attachments

• 27 KB Views: 25
• 559 bytes Views: 22

#### alec_t

##### Well-Known Member
In theory, I think the nested IF expression R=if(i(R1)-9.5,3,if(i(R1)-0.5,2,1)) should give the three values to R. In practice, Spice doesn't like this because there is conflict at the 1A and 10A boundaries.
Consider a rising voltage from V1. As soon as 1A is reached, the resistance jumps from 1 to 2 Ohms, which immediately drops the current below 1A, so R jumps back to 1 Ohm. The outcome is the familiar "Time interval too short" message.
Someone else may know a work-around.

#### ronsimpson

##### Well-Known Member
First I put the "if" inside the resistor. At 5mS the resistance changes

Next I found an example like this:
.param MODE 1
.param FREQ {if(MODE == 0, 1000, if(MODE == 1, 500, if(MODE == 2,250)))}
-----edited-----
I think this is what Alec_t said.

#### alec_t

##### Well-Known Member
I think this is what Alec_t said.
Yes, that's a similar nested IF expression, albeit with static values.

#### eTech

##### Well-Known Member
Hi,

I would like to create a variable resistor as following:

R2=1 if i(R2)<1
R2=2 if 1<i(R2)<10
R2=3 if i(R2)>10

I know that the sintax is:

R=if(condition,valueiftrue,valueiffalse)

What about if I have three intervals?

The .asc file and the IF statement law is also shown in the attached files.

Which is the correct sintax to do that?

Thank you.

Hi again

See below.

In the .func statement, the "if" statement has to resolve to a number. I've added 0.5 at the end.
In the "Limit" statement, I've added arbitrary Min and Max values, 0.1,12, respectively. A discontinuity will occur if not used because at the start of the simulation R equals nothing (NaN). This will produce a "time step too small" error. The "Limit" statement will keep R2 equal to some value between 0.1 and 12.

eT

Status
Not open for further replies.