3 views (last 30 days)

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.

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.)

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);

Walter Roberson
on 8 Jun 2020

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).

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 11 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869004

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869004

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869049

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869049

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869427

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869427

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869815

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_869815

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_870007

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_870007

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_870291

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_870291

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871127

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871127

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871319

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871319

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871563

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871563

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871575

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871575

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871643

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/535894-ressistors-with-small-random-offset-in-simulink#comment_871643

Sign in to comment.