• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

IF statement on LTspice

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

alec_t

Well-Known Member
Most Helpful 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
Most Helpful Member
First I put the "if" inside the resistor. At 5mS the resistance changes
1552061018030.png
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.
See this link: IF
 

alec_t

Well-Known Member
Most Helpful Member

eTech

Active 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

1552089771055.png
 

Latest threads

EE World Online Articles

Loading

 
Top