Electronic – ltspice matlab optimization

ltspiceMATLABoptimization

Has anybody used ltspiceIV in a optimization process using Matlab? I need feedback because I am running my circuit in transient analysis, and when I found the optimal point of my optimization, I ran it again but alone (not in the optimization loop) and I don't get the same values. Also, in a for loop, I obtain NaN (Not a number) value in my results. Is Ltspice suitable for optimization?

I work on electrical machine modeling. It is modelling the machine using equivalent magnetic circuit (which looks like electric circuit with resistor and source). I am then using ltspice coupled with matlab to solve this circuit. I create my netlist using matlab, I solve using ltspice, I collect the results from .raw files using a matlab program and I process the results using also matlab. It works until now. (the end of the question after that)

I am doing optimization so I need to run over and over again ltspice until the algorithm find the optimal point. I want to know if this process of running of multiple time (through optimisation or "for loop") is suitable for ltspice? I found that it can give me false results or doesn't compute the results at certains time (I obtain NaN (Not an Number) values).

In my circuit, there are a lot of controlled source, subcircuit, inductance and resistors.

Best Answer

Here is some code to run a sim from matlab. One option would be to generate a netlist file from the .asc and run that from the command prompt, you could even change the netlist file from matlab and add, delete components or in the least change values.

Another idea I've had is you could also do this with netlists and write the netlists from matlab and even do genetic optimization with circuits by having matlab write the circuit for you, then run the circuit, then check the result in matlab.

Anyway here is the code, I changed some names and you don't have the .asc files or the write nets, so the code will need some retooling but here is an example of how to get the code out.

You'll also need the function Ltspice2Matlab within the scope of this code. An advantage is I don't think the function generates Nan's and has a good timebase that it returns. If it does you can simply replace the Nan's with the previous value or and average of the previous value and the next value using isnan

status1 = system('C:\...\LTspiceIV\scad3.exe -Run -b blah.asc')

comparerun = LTspice2Matlab('blah.raw');

status2 = system('C:\...\LTspiceIV\scad3.exe -Run -b blah2.asc')

comparerun2 = LTspice2Matlab('blah2.raw');

%get some data from netlists out of the raw file
cVres = comparerun.variable_mat(find(strcmp(comparerun.variable_name_list,'V(one)')),:); 
cVzerostage = comparerun.variable_mat(find(strcmp(comparerun.variable_name_list,'V(two)')),:);
cVthree= comparerun.variable_mat(find(strcmp(comparerun.variable_name_list,'V(three)')),:);


% figure;plot(baserun.time_vect,Vthree)


ylabt = 'Voltage';
tittx = 'title goes here';
h = figure;subplot(20,1,1:12),plot(Vres,cVthree),hold on,plot(cVres,cVthree,'r')
legend('Origonal Run','Compare Run');
title(tittx);
xlabel('xlabel')
ylabel(ylabt);

% if you want to compare the two runs with a residual
interpV4thstage = interp1(comparerun.time_vect,cVthree,baserun.time_vect);
subplot(20,1,17:20),plot(Vres,Vthree-interpV4thstage,'m')
title('Residual');
xlabel('xlabel')
ylabel(ylabt);