You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
ressistors with small random offset in SIMULINK
3 views (last 30 days)
Show older comments
Hello , I want to have 10 resistors with a littles offset in resistance by random number arounk 1kohm
i have itried to use matlab simuling VAriable ressistor.
But i dont see how i enter there some random offset in the value.
Thanks.
11 Comments
Walter Roberson
on 27 May 2020
Under what circumstances should the random resistance change? For example can it be chosen once at the beginning of the run and will hold for the duration of the run? Or should it change (say) after every 3 seconds? Or do you want it to be different "every time" it is used (if so are you using continuous or discrete?) Or are you trying to model increased resistance as temperatures increase as the circuit gets warmer?
Walter Roberson
on 28 May 2020
Use a MATLAB function that does set_param() to modify the resistances .
fima v
on 28 May 2020
Hello walter,If i have 10 ressistors each one is 10Kohm,this is how i imagine it in matlab code.
in the matlab simulink using set_param(), how can i implement it?
Thanks.
R_basic=10000*ones(10);
R_random=R_basic+randn(1,10);
Walter Roberson
on 28 May 2020
That should be ones(1,10);
I am having difficulty figuring out which block you are looking at. The blocks I find with similar descriptions do not have the same icon, but the icon can be important as it gives information about which Domain you are working with. The PS on the icon hints to me you might be using Specialized Power Systems (SPS), but the discussion I find for variable resistors for SPS use quite a different approach.
fima v
on 28 May 2020
Hello walter,could you give me a resistor model where there is such option can be done?
Thanks.
Walter Roberson
on 28 May 2020
Which Domain are you using? Are you using pure Simulink? Simscape Electrical? Specialized Power Systems?
Walter Roberson
on 28 May 2020
Walter Roberson
on 28 May 2020
I am having difficulty finding resistors in pure simulink. I find resistors in
- RF Blockset
- Simscape Electrical
- Specialized Power Systems
fima v
on 28 May 2020
Hello Wolter , i am not sure regarding what exact simulink i am using, ithink i have here RF blockset.
could you please say on this platform how to define random argument in the resistor value?
Thanks.
Accepted Answer
Walter Roberson
on 29 May 2020
Edited: Walter Roberson
on 29 May 2020
The attached model is adapted (solver parameters updated) from the File Exchange Contribution
It happens to have 11 resistors from Specialized Power Systems (part of Simscape Electrical)
The resistances can be randomized using
for K = 1 : 11
pn = sprintf('SERIES_PARALLEL_RESISTORS/R_%d', K);
set_param(pn, 'Resistance', sprintf('%.5g', 1000 + randn(1)));
end
Here, SERIES_PARALLEL_RESISTORS is the model name, and each of the resistors happened to be named R_ followed by a number. Resistance is the name of the parameter used in SPS for resistance (some of the other forms of resistor in Simulink use 'R' as the name for the resistance.)
It was saved with R2020a (you did not indicate which version you are using.)
20 Comments
fima v
on 29 May 2020
Hello Walter i am using 2019a, from the code bellow i recognise that you write into a file.
What should i do in order to see those resistors physicly on simulink?
Thanks.
Walter Roberson
on 29 May 2020
Model for R2019a attached.
modname = 'SERIES_PARALLEL_RESISTORS19a'
open_system(modname);
for K = 1 : 11
pn = sprintf('%s/R_%d', modname, K);
set_param(pn, 'Resistance', sprintf('%.5g', 1000 + randn(1)));
end
If you know double-click on R_1 in the model, you will see that it has a resistance parameter that is not exactly 1000.
That loop is not writing into a file: it is adjusting the parameters of the loaded model. If you were to run the loop part if it again and then look at R_1 again you will see a different resistance.
fima v
on 30 May 2020
Hello Walter, i am not used to running code in parralel with simulink.
in simulink i know to grab blocks and make circuit out of it.
Where shoud i put this code in simulink?
I am having problem to immagine the procedure, assuming i run this code with this file in regular matlab window.
Then i enter simulink, what to do next in the simulink window to have 10 resistor of random values?
Thanks.
Walter Roberson
on 30 May 2020
Easiest way: instead of using Simulink to Run the model, instead, In a MATLAB script (or function)
modname = 'SERIES_PARALLEL_RESISTORS19a'
open_system(modname);
for K = 1 : 11
pn = sprintf('%s/R_%d', modname, K);
set_param(pn, 'Resistance', sprintf('%.5g', 1000 + randn(1)));
end
sim(modname);
But it is also possible to set something up that will run in Simulink. I was not able to figure out how to set up the proper callbacks using the GUI, so once you have created the model, once do the following at the command line:
modname = 'SERIES_PARALLEL_RESISTORS19a'
open_system(modname);
for K = 1 : 11; set_param(sprintf('%s/R_%d', modname, K), 'InitFcn', 'set(gcbh, ''Resistance'', string(1000+randn()))'); end
Now you can save the model at the Simulink level.
Once the above is done, each time you Run the model in Simulink, each of the resistors will run initialization code that sets its resistance randomly.
You can of course change the formula 1000+randn() to whatever is appropriate for your purposes.
The same technique can be used with resistors from other domains such as RF Blocksets, but you may need to change the name of the parameter from Resistance to R
fima v
on 31 May 2020
Hello Walter, I need to do this trick for DC voltage source create 10 DC souces with 0.5+0.1*rand(1) in them
and i need to have both of my random resistor and random DC voltage sources together so i will build something combined.
Should i use the same file?
what should i do to add 10 random DC voltage sources to it?
Thanks.
Walter Roberson
on 31 May 2020
I added some DC Voltage Source. I deliberately renamed them to have DCV_ names to make it easier.
So the procedure is to add your resistors and voltages sources to your simulink model, then run code similar to the below:
modname = 'SERIES_PARALLEL_RESISTORS_NoVoltage_19a'
open_system(modname);
for K = 1 : 11; set_param(sprintf('%s/R_%d', modname, K), 'InitFcn', 'set(gcbh, ''Resistance'', string(1000+randn()))'); end
for K = 1 : 3; set_param(sprintf('%s/DCV_%d', modname, K), 'InitFcn', 'set(gcbh, ''Amplitude'', string(0.5+0.1*randn()))'); end
then save the model. After that, each time your Run the model, the voltages and resistances will be randomized.
Walter Roberson
on 1 Jun 2020
Edited: Walter Roberson
on 1 Jun 2020
Are you really going to create and route 255 or 510 components by hand in the graphical interface? It would probably be easier to add by way of program, provided that you had an algorithm for which is connected to which.
fima v
on 1 Jun 2020
Hello Walter, I need to do an 8 bit converter 2^8-1=256-1.
So is there some method you can recomend me where i could drag and drop a component an it will randomly puld a value as you implemented?
Thanks.
Walter Roberson
on 1 Jun 2020
An 8 bit converter? As in a resistance ladder to determine an input voltage?
fima v
on 2 Jun 2020
Hello Walter, yes to conver analog to digital signal .but i need voltage and resistor variance(not idial components
i have found in the following manual tolerance option for the resistor.
But for DC source i coudnt find such component with tollerance.
Walter Roberson
on 2 Jun 2020
what is your diagram? The architectures that I find for flash ADC ("thermometer style") use only one voltage source.
fima v
on 2 Jun 2020
Edited: fima v
on 2 Jun 2020
Hello Walter, Yes thats the idial model, the non idial model uses
resistor offset and comparator voltage offset.
in a component named "resistor" i have found an option called "tolerance" which exactly does what we need.
but i dont have any component the does "tollerance with voltage source"
I wish i knew how you did those 10 random voltage sources so i could do a version of 256 of those.
Could you please tell me a method how to accomplish such thing?
Thanks.
Walter Roberson
on 2 Jun 2020
I posted the exact code that I used for the randomization.
I started from Chaitanya Jambotkar's existing model just to have something with resistors in it to work with.
Here is some example code that creates and positions DC Voltage Sources and Resistors automatically, and configures them to initialize their parameters randomly at run time. It also shows automatically connecting the output of a DC Voltage to the input of a Resistor.
I did not attempt to build the flash ADC for you; I am just giving you the tools to proceed with.
modname = 'test_model';
N_voltage_sources = 15;
N_resistor = 15;
SL_powergui_block = 'powerlib/powergui';
SL_DCV_block = 'powerlib/Electrical Sources/DC Voltage Source';
SL_resistor_block = 'powerlib/Elements/Series RLC Branch';
DCV_xbase = 50;
DCV_ybase = -150; %yes, negative!
DCV_width = 20;
DCV_height = 35;
DCV_yspacing = 75;
R_xbase = 125;
R_ybase = DCV_ybase;
R_width = 80;
R_height = 35;
R_yspacing = DCV_yspacing;
modfile = sprintf('%s.slx', modname);
if exist(modfile, 'file'); delete(modfile); end
try
h = new_system(modname);
catch
Simulink.BlockDiagram.deleteContents(modname);
end
add_block(SL_powergui_block, sprintf('%s/powergui', modname)); %required solver component
DCV_blocknames = cell(N_voltage_sources, 1);
DCV_ports = cell(N_voltage_sources, 1);
R_blocknames = cell(N_resistor, 1);
R_ports = cell(N_resistor, 1);
xbase = DCV_xbase;
xend = xbase + DCV_width - 1;
for K = 1 : N_voltage_sources
ybase = DCV_ybase + (K-1)*DCV_yspacing;
yend = ybase + DCV_height;
blockname = sprintf('%s/DCV_%d', modname, K);
DCV_blocknames{K} = blockname;
add_block(SL_DCV_block, blockname, ...
'Position', [xbase, ybase, xend, yend], ...
'InitFcn', 'set(gcbh, ''Amplitude'', string(0.5+0.1*randn()))' );
DCV_ports{K} = get_param(blockname, 'PortHandles');
end
xbase = R_xbase;
xend = xbase + R_width - 1;
for K = 1 : N_resistor
ybase = R_ybase + (K-1)*R_yspacing;
yend = ybase + R_height;
blockname = sprintf('%s/R_%d', modname, K);
R_blocknames{K} = blockname;
add_block(SL_resistor_block, blockname, 'BranchType', 'R', ...
'Position', [xbase, ybase, xend, yend], ...
'InitFcn', 'set(gcbh, ''Resistance'', string(1000+randn()))');
R_ports{K} = get_param(blockname, 'PortHandles');
end
for K = 1 : N_voltage_sources
add_line(modname, DCV_ports{K}.RConn, R_ports{K}.LConn);
end
save_system(modname);
open_system(modname);
fima v
on 3 Jun 2020
Hello Walter, The code generated me 255 resistors and 255 voltage sources but they all have the same value.
I have tried to make them with random values by putting 1 inside the randn as shown bellow, still it gives me a constant value in all of them.
Where did i go wrong?
Thanks.
'InitFcn', 'set(gcbh, ''Amplitude'', string(0.5+0.1*randn(1)))' );
'InitFcn', 'set(gcbh, ''Resistance'', string(1000+randn(1)))');
Walter Roberson
on 3 Jun 2020
For reasons I do not currently understand, when the model is saved, InitFcn is being cleared.
The model will work correctly if you use it when it pops up (the open_system), but not if you close that and load it from disk.
This will require further investigation.
fima v
on 3 Jun 2020
Edited: fima v
on 3 Jun 2020
Hello Walter, it works great. unfortunetly when i tried to connect SIMSCAPE opamp(to act as comparator) and a scope to see DC output. these componets do not connect to each other. i tried to use a ps-simulink converter as shown in the link post bellow. and still i could not connect them together
Where did i go wrong?
Thanks.
Walter Roberson
on 5 Jun 2020
I extended the code to show adding Voltage Measurements, Current Measurements, and Scopes.
The positioning of the Voltage Measurements and Current Measurements, and Scope,could use some improvement -- I positioned them too closely together, so the connecting lines do not show up well.
I have not worked out Op Amps yet. Do you actually want Op Amps, or do you want comparitors? What should the output be for the case where the comparison succeeds?
modname = 'test_model';
N_voltage_sources = 15;
N_resistor = 15;
N_scopes = 1;
N_VM = 1;
N_CM = 1;
SL_powergui_block = 'powerlib/powergui';
SL_DCV_block = 'powerlib/Electrical Sources/DC Voltage Source';
SL_resistor_block = 'powerlib/Elements/Series RLC Branch';
SL_VM_block = 'powerlib/Measurements/Voltage Measurement';
SL_CM_block = 'powerlib/Measurements/Current Measurement';
SL_Scope_block = 'simulink/Sinks/Scope';
DCV_xbase = 50;
DCV_ybase = -150; %yes, negative!
DCV_width = 20;
DCV_height = 35;
DCV_yspacing = 75;
R_xbase = 125;
R_ybase = DCV_ybase;
R_width = 80;
R_height = 35;
R_yspacing = DCV_yspacing;
modfile = sprintf('%s.slx', modname);
if exist(modfile, 'file'); delete(modfile); end
try
h = new_system(modname);
catch
Simulink.BlockDiagram.deleteContents(modname);
end
add_block(SL_powergui_block, sprintf('%s/powergui', modname)); %required solver component
DCV_blocknames = cell(N_voltage_sources, 1);
DCV_ports = cell(N_voltage_sources, 1);
R_blocknames = cell(N_resistor, 1);
R_ports = cell(N_resistor, 1);
VM_blocknames = cell(N_VM, 1);
VM_ports = cell(N_VM, 1);
CM_blocknames = cell(N_CM, 1);
CM_ports = cell(N_CM, 1);
Scope_blocknames = cell(N_scopes, 1);
Scope_ports = cell(N_scopes, 1);
xbase = DCV_xbase;
xend = xbase + DCV_width - 1;
for K = 1 : N_voltage_sources
ybase = DCV_ybase + (K-1)*DCV_yspacing;
yend = ybase + DCV_height;
blockname = sprintf('%s/DCV_%d', modname, K);
DCV_blocknames{K} = blockname;
add_block(SL_DCV_block, blockname, ...
'Position', [xbase, ybase, xend, yend], ...
'InitFcn', 'set(gcbh, ''Amplitude'', string(0.5+0.1*randn()))' );
DCV_ports{K} = get_param(blockname, 'PortHandles'); %- (L) || + (R)
end
xbase = DCV_xbase + 50;
xend = xbase + R_width - 1;
for K = 1 : N_resistor
ybase = R_ybase + (K-1)*R_yspacing;
yend = ybase + R_height;
blockname = sprintf('%s/R_%d', modname, K);
R_blocknames{K} = blockname;
add_block(SL_resistor_block, blockname, 'BranchType', 'R', ...
'Position', [xbase, ybase, xend, yend], ...
'InitFcn', 'set(gcbh, ''Resistance'', string(1000+randn()))');
R_ports{K} = get_param(blockname, 'PortHandles'); %L || R
end
xbase = R_xbase + 50;
for K = 1 : N_VM
ybase = R_ybase + (K-1)*R_yspacing;
yend = ybase + R_height;
blockname = sprintf('%s/VM_%d', modname, K);
VM_blocknames{K} = blockname;
add_block(SL_VM_block, blockname, ...
'Position', [xbase, ybase, xbase + 35, yend]);
VM_ports{K} = get_param(blockname, 'PortHandles'); % + - || outport
end
xbase = R_xbase + 100;
for K = 1 : N_CM
ybase = DCV_ybase + (K-1)*R_yspacing;
yend = ybase + R_height;
blockname = sprintf('%s/CM_%d', modname, K);
CM_blocknames{K} = blockname;
add_block(SL_CM_block, blockname, ...
'Position', [xbase, ybase, xbase+35, yend]); % + || outport -
CM_ports{K} = get_param(blockname, 'PortHandles');
end
xbase = R_xbase + 150;
for K = 1 : N_scopes
ybase = DCV_ybase + (K-1)*R_yspacing;
yend = ybase + R_height;
blockname = sprintf('%s/Scope_%d', modname, K);
Scope_blocknames{K} = blockname;
add_block(SL_Scope_block, blockname, ...
'Position', [xbase, ybase, xbase + 35, yend], ...
'NumInputPorts', '2');
Scope_ports{K} = get_param(blockname, 'PortHandles');
end
for K = 1 : N_voltage_sources
add_line(modname, DCV_ports{K}.RConn, R_ports{K}.LConn);
end
add_line(modname, R_ports{1}.RConn, VM_ports{1}.LConn(1));
add_line(modname, VM_ports{1}.LConn(2), CM_ports{1}.LConn);
add_line(modname, CM_ports{1}.RConn, DCV_ports{1}.LConn);
add_line(modname, VM_ports{1}.Outport, Scope_ports{1}.Inport(1));
add_line(modname, CM_ports{1}.Outport, Scope_ports{1}.Inport(2));
save_system(modname);
fima v
on 5 Jun 2020
Edited: fima v
on 5 Jun 2020
Hello Walter, My comperator needs to have input noise 10nV/sqrt(Hz), bandwidth(100MHZ) ,amplitude(200) , the theshhold of the comperator is 0.6v.
VDD=1V VCC=0V .
so if for example (V_plus-V_minus)*Amplitude>0.6 the output is 1V else 0V.
I have tried to used the simscape bandlimited component to achieve those properties.
but then i couldnt connect with the other components as shown above.
If you could make N such comparators , it would be great.
Thanks you very much.
Walter Roberson
on 8 Jun 2020
There does not appear to be any one block to implement what you describe. See however https://www.mathworks.com/help/physmod/sps/examples/op-amp-with-noise.html
More Answers (1)
Fangjun Jiang
on 27 May 2020
Edited: Fangjun Jiang
on 28 May 2020
This "Variable Resistor" is not the right one to use. This "Variable Resistor" represents a physical variable resistor in the lab where you can move a handle or turn a knob to adjust the value of the resistor. The "PS" port of this block can be connect to a motor (for example) to move the handle or turn the knob thus change the resistor.
To implement what you want, you just need to set "R=10e3-10+20*rand(10,1)" in workspace and drop 10 regular resistor blocks in your model, set the parameter for the first resistor to be R(1), ... and the last one to be R(10).
1 Comment
fima v
on 28 May 2020
could you please show a print screen of "R=10e3-10+20*rand(10,1) in workspace"?
and drop 10 resistors.i am having trouble visualise it.
Thabks.
See Also
Categories
Find more on Detect and Diagnose Faults in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)