# Constrained Global Optimization Problem with MultiStart, GA and HybridFunction using Parallel Processing whithout Step

3 views (last 30 days)

Show older comments

Hello,

I am interested in using a genetic algorithm approach to fitting measurement data to a function of 3 variables with 4 unknown coefficients.

I am not concerned with the computational time required for the genetic approach, as, for now, I am just trying to develop a methodology for fitting complex, non-linear/non-smooth, functions using the various features available in Matlab's Global Optimization toolbox. The complexity and non-linearity of my function (presented below) will greatly increase in the near future.

The function which I am trying to fit my data to is defined as following:

VALUE=a1*x+a2*ln(y)+a3*abs(z)+a4

With: a1,a2,a3,a4 being the regression model's unknown coefficients

The following optimization constraints need to be imposed on the coefficients of the function:

a3<0; a4>0; a1*a2<0;

The data to which I am trying to fit my function to is presented in the attached SampleData.csv file. The file contains column labels which define the measured value and x,y,z parameters.

My initial guesses for the values of a1,a2,a3,a4 are defined as a row vector: [0,0,0,0].

I would like to learn how to set this problem up using MultiStart and parallel processing. I would like to use parallel processing for the MultiStart process, as well as to include a Hybrid Function, if such an operation is possible.

The work flow which I would like to achieve should be similar to:

1. Define Function (referring to x,y,z data contained in columns of SampleData.csv file) :

function [ Value ] = FittingFunction(x,data)

Value=x(1).*data(:,2)+...

x(2).*log(data(:,3))+...

x(3).*abs(data(:,4))+...

x(4);

end

I would prefer to not use an anonymous function as the complexity of the model to which I will be fitting my future data to will only increase and an anonymous function would be messy to adjust as the regression model matures.

2.Define Initial Guess Row Vector x=[0,0,0,0]

3.Define optimization problem (referring to value data & x,y,z data contained in columns of SampleData.csv file) as a minimization problem trying to minimize a cost function defined as:

Cost=sum((FittingFunction(x,data)-data(:,1)).^2);

4. Pass optimization problem to a Genetic Algorithm routine with a hybrid function included using fminunc.

5. Create a MultiStart Optimization Object and Start a pool of workers.

lowerBounds for MultiStart =[-Inf,-Inf,-Inf,-Inf]; upperBounds for MultiStart =[Inf,Inf,Inf,Inf];

Number of MultiStart Iterations= 5.

5. Run optimization in parallel.

I realize that the problem which I defined is quite complex in its definition. I am not an expert user of Matlab and may not realize that my approach to solving this problem and the requirements (using the genetic algorithm, parallel processing, constraining coefficient ranges, and doing all of this using a MultiStart approach) for the fitting process may be unachievable.

I am using the serial port to read numbers from the arduino encoder. I did drawing a chart in app designer but in the X axis I only have the number of the next displayed number. I would like to see the time after which the number appeared in the X axis. The first number is e.g. 5 and in the x axis its time is 0, the next number 6 in the x axis appeared after 120ms and wants this time on the X axis.

delete(instrfind({'Port'},{app.PORT}))

app.x=serial(app.PORT,'BAUD',9600);

flushinput(app.x);

fopen(app.x); % otwarcie portu szeregowego

for i=1:app.Zakres.Value

drawnow

if app.Z == 1

break

end

h = str2num(fscanf(app.x));

app.aPolozenie.Value = h;

s = [s,h];

plot(app.UIAxes,s);

app.UIAxes.YLim = [ app.Od.Value app.Do.Value];

app.UIAxes.XLim = [0 app.Zakres.Value];

pause(.0001);

app.Zatrzymaj

end

end

This is my current status:

- I do not know how to set up the fitting routine to use the Genetic Algorithm; my code is currently using lsqcurvefit, because this was the only way that I could figure out how to instantiate the matlab optimization problem object using the createOptimProblem() constructor.
- I understand that lsqcurvefit cannot perform constrained optimization, but the Genetic Algorithm approach can do that; any help with setting up this problem using the genetic approach would be greatly appreciated, even if it is without the use of MultiStart for now.
- As I am currently not using the Genetic Algorithm, I am not forcing the optimization solver to use the 'HybridFunction','on' option.
- I am aware of the fact that the Global Optimization Toolbox has two options for finding global minima: GlobalSearch and MultiStart. I have decided to use MultiStart because according to the following article: http://www.mathworks.com/help/gads/how-globalsearch-and-multistart-work.html#bsc9eec MultiStart Can be setup in parallel on a multicore processor, while GlobalSearch cannot.
- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

When installing the MinGW compiler, the environment variable MW_MINGW64_LOC is set with the path to the MinGW installation folder. On some systems where users have limited privileges, there may be policies in place preventing environment variables from being set. If this environment variable is not staying set following the installation, or after restarting MATLAB, the behavior described above may occur.

Here is my current code (requires the data.mat file attached to my posting):

clc

clear variables

%%Loading Measurement Data File

load('data.mat')

%%Defining function to which the data will be fit to

% I would like to move away from using the anonymous function,

% and move to using the function file defined in the directory but I don't

% know how to pass a function file to the createOptimProblem function call.

fitfcn1 = @(x,data)x(1).*data.x+x(2).*log(data.y)+abs(data.z).*x(3)+x(4);

%%Defining lower and upper bounds for MultiStart procedure

lb1 = [-Inf,-Inf,-Inf,-Inf];

ub1 = [Inf,Inf,Inf,Inf];

%%Creating row vector for intial guess to optimization routine

p01 = 0*ones(1,4);

%%Defining optimization problem object

% Currently running lsqcurvefit, but I need to move to the genetic

% algorythm approach. I do not understand how to setup a genetic alorythm

% data fitting routine in matlab using the GA functionality built into the

% global optimization toolbox.

problem1 = createOptimProblem('lsqcurvefit','x0',p01,'objective',fitfcn1,...

'lb',lb1,'ub',ub1,'xdata',data,'ydata',data.value);

%%Creating live fitting progress plot

ms1 = MultiStart('PlotFcns',@gsplotbestf);

%%Running MultiStart Optimization Routine

[xmulti1,errormulti1] = run(ms1,problem1,5)

This is my current status:

- I do not know how to set up the fitting routine to use the Genetic Algorithm; my code is currently using lsqcurvefit, because this was the only way that I could figure out how to instantiate the matlab optimization problem object using the createOptimProblem() constructor.
- I understand that lsqcurvefit cannot perform constrained optimization, but the Genetic Algorithm approach can do that; any help with setting up this problem using the genetic approach would be greatly appreciated, even if it is without the use of MultiStart for now.
- As I am currently not using the Genetic Algorithm, I am not forcing the optimization solver to use the 'HybridFunction','on' option.
- I am aware of the fact that the Global Optimization Toolbox has two options for finding global minima: GlobalSearch and MultiStart. I have decided to use MultiStart because according to the following article: http://www.mathworks.com/help/gads/how-globalsearch-and-multistart-work.html#bsc9eec MultiStart Can be setup in parallel on a multicore processor, while GlobalSearch cannot.
- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

Can someone help on this? I'm sure it is straight forward but getting stumped. I would simply like to add two matrices together, element-wise, ignoring the NaNs. I realize one solution is to replace NaNs with zeros but I have some reported zeros that I want to be able to later identify. A solution to that is to set any real zeros to extremely small numbers. Rather than doing the above I am wondering if there is any other way to ignoring the NaNs -- I know of sum(...,'omitnan') and nansum() but they work on one matrix or if I concatenate the two matrices together then they aggregate either all rows or all columns depending on the dimension I select.

For example: A = ones(10,3); B = ones(10,3);

% inserting some NaNs and zeros A([1 6 10],1) = NaN; A([3 7],[2 3]) = NaN; B([3 7],2) = 0;

C = A + B;

I would like C = [1 2 2; 2 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2]

I realize that the problem which I defined is quite complex in its definition. I am not an expert user of Matlab and may not realize that my approach to solving this problem and the requirements (using the genetic algorithm, parallel processing, constraining coefficient ranges, and doing all of this using a MultiStart approach) for the fitting process may be unachievable.

After installing the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package, the command

>> mex -setup

reports that the compiler is correctly installed. However, when I restart MATLAB, the same command does not detect it as installed anymore.

When I check the Add-On Explorer, or run the command

>> matlabshared.supportpkg.getInstalled

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

When installing the MinGW compiler, the environment variable MW_MINGW64_LOC is set with the path to the MinGW installation folder. On some systems where users have limited privileges, there may be policies in place preventing environment variables from being set. If this environment variable is not staying set following the installation, or after restarting MATLAB, the behavior described above may occur.

Running the 'mex -setup' command in verbose mode

>> mex -setup -v

will display whether the MW_MINGW64_LOC environment variable is set. If this is not the case, the output will look as shown below:

... Looking for compiler 'MinGW64 Compiler (C)' ...

... Looking for environment variable 'MW_MINGW64_LOC' ...No.

The solution is to have an administrator set the environment variable. On Windows 10, you find this at:

Control Panel > System and Security > System > Advanced system settings > Environment Variables... > System variables: New...

The environment variable should have the following name and value:

Variable Name: MW_MINGW64_LOC

Variable Value: <SupportPackageRoot>\3P.instrset\mingw_w64.instrset

where <SupportPackage Root> is the output received for the command:

>> matlabshared.supportpkg.getSupportPackageRoot

Before the loop:

start_time = datetime('now');

rel_times = [];

In the loop, after

s = [s,h];

add

dt = milliseconds(datetime('now') - start_time);

rel_times = [rel_times, dt];

and change

plot(app.UIAxes,s);

to

plot(app.UIAxes, rel_times, s);

Note that performance of your application will get worse and worse over time, due to the way that you keep extending the s and rel_time arrays. Consider using a fixed-size buffer. Consider using animatedline() especially with a maximum number of points.

The Fixed-Point Tool collects ranges and proposes data types by data type groups rather than individual blocks.

A Data type group is a collection of block paths that need to have the same data type. If they don’t, Simulink will throw a data type mismatch error.

For instance, the output of the gain and the input to the subsystem need to have the same data type (they belong to the same data type group). Otherwise Simulink will throw a data type mismatch error. You can try this by changing the data type of the gain in the attached model to single.

For more details on the data type group, please refer to the documentation link below:

All the shared ranges are an aggregation of the data type group ranges. In this case, shared simulation range is the aggregate of the simulation ranges of the data type group and similarly the shared design range is an aggregate of the design ranges of the group.

Fixed Point Tool proposes a data type that can accommodate all these shared ranges for the group.You can infact click the link at the bottom of the Result Details pane to highlight all the members of the data type group.

I am not concerned with the computational time required for the genetic approach, as, for now, I am just trying to develop a methodology for fitting complex, non-linear/non-smooth, functions using the various features available in Matlab's Global Optimization toolbox. The complexity and non-linearity of my function (presented below) will greatly increase in the near future.

The function which I am trying to fit my data to is defined as following:

VALUE=a1*x+a2*ln(y)+a3*abs(z)+a4

With: a1,a2,a3,a4 being the regression model's unknown coefficients

The following optimization constraints need to be imposed on the coefficients of the function:

a3<0; a4>0; a1*a2<0;

The data to which I am trying to fit my function to is presented in the attached SampleData.csv file. The file contains column labels which define the measured value and x,y,z parameters.

My initial guesses for the values of a1,a2,a3,a4 are defined as a row vector: [0,0,0,0].

I would like to learn how to set this problem up using MultiStart and parallel processing. I would like to use parallel processing for the MultiStart process, as well as to include a Hybrid Function, if such an operation is possible.

The work flow which I would like to achieve should be similar to:

1. Define Function (referring to x,y,z data contained in columns of SampleData.csv file) :

function [ Value ] = FittingFunction(x,data)

Value=x(1).*data(:,2)+...

x(2).*log(data(:,3))+...

x(3).*abs(data(:,4))+...

x(4);

end

I would prefer to not use an anonymous function as the complexity of the model to which I will be fitting my future data to will only increase and an anonymous function would be messy to adjust as the regression model matures.

2.Define Initial Guess Row Vector x=[0,0,0,0]

3.Define optimization problem (referring to value data & x,y,z data contained in columns of SampleData.csv file) as a minimization problem trying to minimize a cost function defined as:

Cost=sum((FittingFunction(x,data)-data(:,1)).^2);

4. Pass optimization problem to a Genetic Algorithm routine with a hybrid function included using fminunc.

5. Create a MultiStart Optimization Object and Start a pool of workers.

lowerBounds for MultiStart =[-Inf,-Inf,-Inf,-Inf]; upperBounds for MultiStart =[Inf,Inf,Inf,Inf];

Number of MultiStart Iterations= 5.

5. Run optimization in parallel.

I realize that the problem which I defined is quite complex in its definition. I am not an expert user of Matlab and may not realize that my approach to solving this problem and the requirements (using the genetic algorithm, parallel processing, constraining coefficient ranges, and doing all of this using a MultiStart approach) for the fitting process may be unachievable.

I am using the serial port to read numbers from the arduino encoder. I did drawing a chart in app designer but in the X axis I only have the number of the next displayed number. I would like to see the time after which the number appeared in the X axis. The first number is e.g. 5 and in the x axis its time is 0, the next number 6 in the x axis appeared after 120ms and wants this time on the X axis.

delete(instrfind({'Port'},{app.PORT}))

app.x=serial(app.PORT,'BAUD',9600);

flushinput(app.x);

fopen(app.x); % otwarcie portu szeregowego

for i=1:app.Zakres.Value

drawnow

if app.Z == 1

break

end

h = str2num(fscanf(app.x));

app.aPolozenie.Value = h;

s = [s,h];

plot(app.UIAxes,s);

app.UIAxes.YLim = [ app.Od.Value app.Do.Value];

app.UIAxes.XLim = [0 app.Zakres.Value];

pause(.0001);

app.Zatrzymaj

end

end

This is my current status:

- I do not know how to set up the fitting routine to use the Genetic Algorithm; my code is currently using lsqcurvefit, because this was the only way that I could figure out how to instantiate the matlab optimization problem object using the createOptimProblem() constructor.
- I understand that lsqcurvefit cannot perform constrained optimization, but the Genetic Algorithm approach can do that; any help with setting up this problem using the genetic approach would be greatly appreciated, even if it is without the use of MultiStart for now.
- As I am currently not using the Genetic Algorithm, I am not forcing the optimization solver to use the 'HybridFunction','on' option.
- I am aware of the fact that the Global Optimization Toolbox has two options for finding global minima: GlobalSearch and MultiStart. I have decided to use MultiStart because according to the following article: http://www.mathworks.com/help/gads/how-globalsearch-and-multistart-work.html#bsc9eec MultiStart Can be setup in parallel on a multicore processor, while GlobalSearch cannot.
- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

Here is my current code (requires the data.mat file attached to my posting):

clc

clear variables

%%Loading Measurement Data File

load('data.mat')

%%Defining function to which the data will be fit to

% I would like to move away from using the anonymous function,

% and move to using the function file defined in the directory but I don't

% know how to pass a function file to the createOptimProblem function call.

fitfcn1 = @(x,data)x(1).*data.x+x(2).*log(data.y)+abs(data.z).*x(3)+x(4);

%%Defining lower and upper bounds for MultiStart procedure

lb1 = [-Inf,-Inf,-Inf,-Inf];

ub1 = [Inf,Inf,Inf,Inf];

%%Creating row vector for intial guess to optimization routine

p01 = 0*ones(1,4);

%%Defining optimization problem object

% Currently running lsqcurvefit, but I need to move to the genetic

% algorythm approach. I do not understand how to setup a genetic alorythm

% data fitting routine in matlab using the GA functionality built into the

% global optimization toolbox.

problem1 = createOptimProblem('lsqcurvefit','x0',p01,'objective',fitfcn1,...

'lb',lb1,'ub',ub1,'xdata',data,'ydata',data.value);

%%Creating live fitting progress plot

ms1 = MultiStart('PlotFcns',@gsplotbestf);

%%Running MultiStart Optimization Routine

[xmulti1,errormulti1] = run(ms1,problem1,5)

This is my current status:

- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

Can someone help on this? I'm sure it is straight forward but getting stumped. I would simply like to add two matrices together, element-wise, ignoring the NaNs. I realize one solution is to replace NaNs with zeros but I have some reported zeros that I want to be able to later identify. A solution to that is to set any real zeros to extremely small numbers. Rather than doing the above I am wondering if there is any other way to ignoring the NaNs -- I know of sum(...,'omitnan') and nansum() but they work on one matrix or if I concatenate the two matrices together then they aggregate either all rows or all columns depending on the dimension I select.

For example: A = ones(10,3); B = ones(10,3);

% inserting some NaNs and zeros A([1 6 10],1) = NaN; A([3 7],[2 3]) = NaN; B([3 7],2) = 0;

C = A + B;

I would like C = [1 2 2; 2 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2]

After installing the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package, the command

>> mex -setup

reports that the compiler is correctly installed. However, when I restart MATLAB, the same command does not detect it as installed anymore.

When I check the Add-On Explorer, or run the command

>> matlabshared.supportpkg.getInstalled

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

When installing the MinGW compiler, the environment variable MW_MINGW64_LOC is set with the path to the MinGW installation folder. On some systems where users have limited privileges, there may be policies in place preventing environment variables from being set. If this environment variable is not staying set following the installation, or after restarting MATLAB, the behavior described above may occur.

Running the 'mex -setup' command in verbose mode

>> mex -setup -v

will display whether the MW_MINGW64_LOC environment variable is set. If this is not the case, the output will look as shown below:

... Looking for compiler 'MinGW64 Compiler (C)' ...

... Looking for environment variable 'MW_MINGW64_LOC' ...No.

The solution is to have an administrator set the environment variable. On Windows 10, you find this at:

Control Panel > System and Security > System > Advanced system settings > Environment Variables... > System variables: New...

The environment variable should have the following name and value:

Variable Name: MW_MINGW64_LOC

Variable Value: <SupportPackageRoot>\3P.instrset\mingw_w64.instrset

where <SupportPackage Root> is the output received for the command:

>> matlabshared.supportpkg.getSupportPackageRoot

Before the loop:

start_time = datetime('now');

rel_times = [];

In the loop, after

s = [s,h];

add

dt = milliseconds(datetime('now') - start_time);

rel_times = [rel_times, dt];

and change

plot(app.UIAxes,s);

to

plot(app.UIAxes, rel_times, s);

Note that performance of your application will get worse and worse over time, due to the way that you keep extending the s and rel_time arrays. Consider using a fixed-size buffer. Consider using animatedline() especially with a maximum number of points.

The Fixed-Point Tool collects ranges and proposes data types by data type groups rather than individual blocks.

A Data type group is a collection of block paths that need to have the same data type. If they don’t, Simulink will throw a data type mismatch error.

For instance, the output of the gain and the input to the subsystem need to have the same data type (they belong to the same data type group). Otherwise Simulink will throw a data type mismatch error. You can try this by changing the data type of the gain in the attached model to single.

For more details on the data type group, please refer to the documentation link below:

All the shared ranges are an aggregation of the data type group ranges. In this case, shared simulation range is the aggregate of the simulation ranges of the data type group and similarly the shared design range is an aggregate of the design ranges of the group.

Fixed Point Tool proposes a data type that can accommodate all these shared ranges for the group.You can infact click the link at the bottom of the Result Details pane to highlight all the members of the data type group.

I am not concerned with the computational time required for the genetic approach, as, for now, I am just trying to develop a methodology for fitting complex, non-linear/non-smooth, functions using the various features available in Matlab's Global Optimization toolbox. The complexity and non-linearity of my function (presented below) will greatly increase in the near future.

The function which I am trying to fit my data to is defined as following:

VALUE=a1*x+a2*ln(y)+a3*abs(z)+a4

With: a1,a2,a3,a4 being the regression model's unknown coefficients

The following optimization constraints need to be imposed on the coefficients of the function:

a3<0; a4>0; a1*a2<0;

The data to which I am trying to fit my function to is presented in the attached SampleData.csv file. The file contains column labels which define the measured value and x,y,z parameters.

My initial guesses for the values of a1,a2,a3,a4 are defined as a row vector: [0,0,0,0].

I would like to learn how to set this problem up using MultiStart and parallel processing. I would like to use parallel processing for the MultiStart process, as well as to include a Hybrid Function, if such an operation is possible.

The work flow which I would like to achieve should be similar to:

1. Define Function (referring to x,y,z data contained in columns of SampleData.csv file) :

function [ Value ] = FittingFunction(x,data)

Value=x(1).*data(:,2)+...

x(2).*log(data(:,3))+...

x(3).*abs(data(:,4))+...

x(4);

end

I would prefer to not use an anonymous function as the complexity of the model to which I will be fitting my future data to will only increase and an anonymous function would be messy to adjust as the regression model matures.

2.Define Initial Guess Row Vector x=[0,0,0,0]

3.Define optimization problem (referring to value data & x,y,z data contained in columns of SampleData.csv file) as a minimization problem trying to minimize a cost function defined as:

Cost=sum((FittingFunction(x,data)-data(:,1)).^2);

4. Pass optimization problem to a Genetic Algorithm routine with a hybrid function included using fminunc.

5. Create a MultiStart Optimization Object and Start a pool of workers.

lowerBounds for MultiStart =[-Inf,-Inf,-Inf,-Inf]; upperBounds for MultiStart =[Inf,Inf,Inf,Inf];

Number of MultiStart Iterations= 5.

5. Run optimization in parallel.

I am using the serial port to read numbers from the arduino encoder. I did drawing a chart in app designer but in the X axis I only have the number of the next displayed number. I would like to see the time after which the number appeared in the X axis. The first number is e.g. 5 and in the x axis its time is 0, the next number 6 in the x axis appeared after 120ms and wants this time on the X axis.

delete(instrfind({'Port'},{app.PORT}))

app.x=serial(app.PORT,'BAUD',9600);

flushinput(app.x);

fopen(app.x); % otwarcie portu szeregowego

for i=1:app.Zakres.Value

drawnow

if app.Z == 1

break

end

h = str2num(fscanf(app.x));

app.aPolozenie.Value = h;

s = [s,h];

plot(app.UIAxes,s);

app.UIAxes.YLim = [ app.Od.Value app.Do.Value];

app.UIAxes.XLim = [0 app.Zakres.Value];

pause(.0001);

app.Zatrzymaj

end

end

This is my current status:

- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

Here is my current code (requires the data.mat file attached to my posting):

clc

clear variables

%%Loading Measurement Data File

load('data.mat')

%%Defining function to which the data will be fit to

% I would like to move away from using the anonymous function,

% and move to using the function file defined in the directory but I don't

% know how to pass a function file to the createOptimProblem function call.

fitfcn1 = @(x,data)x(1).*data.x+x(2).*log(data.y)+abs(data.z).*x(3)+x(4);

%%Defining lower and upper bounds for MultiStart procedure

lb1 = [-Inf,-Inf,-Inf,-Inf];

ub1 = [Inf,Inf,Inf,Inf];

%%Creating row vector for intial guess to optimization routine

p01 = 0*ones(1,4);

%%Defining optimization problem object

% Currently running lsqcurvefit, but I need to move to the genetic

% algorythm approach. I do not understand how to setup a genetic alorythm

% data fitting routine in matlab using the GA functionality built into the

% global optimization toolbox.

problem1 = createOptimProblem('lsqcurvefit','x0',p01,'objective',fitfcn1,...

'lb',lb1,'ub',ub1,'xdata',data,'ydata',data.value);

%%Creating live fitting progress plot

ms1 = MultiStart('PlotFcns',@gsplotbestf);

%%Running MultiStart Optimization Routine

[xmulti1,errormulti1] = run(ms1,problem1,5)

This is my current status:

- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

Can someone help on this? I'm sure it is straight forward but getting stumped. I would simply like to add two matrices together, element-wise, ignoring the NaNs. I realize one solution is to replace NaNs with zeros but I have some reported zeros that I want to be able to later identify. A solution to that is to set any real zeros to extremely small numbers. Rather than doing the above I am wondering if there is any other way to ignoring the NaNs -- I know of sum(...,'omitnan') and nansum() but they work on one matrix or if I concatenate the two matrices together then they aggregate either all rows or all columns depending on the dimension I select.

For example: A = ones(10,3); B = ones(10,3);

% inserting some NaNs and zeros A([1 6 10],1) = NaN; A([3 7],[2 3]) = NaN; B([3 7],2) = 0;

C = A + B;

I would like C = [1 2 2; 2 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2]

After installing the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package, the command

>> mex -setup

reports that the compiler is correctly installed. However, when I restart MATLAB, the same command does not detect it as installed anymore.

When I check the Add-On Explorer, or run the command

>> matlabshared.supportpkg.getInstalled

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

Running the 'mex -setup' command in verbose mode

>> mex -setup -v

will display whether the MW_MINGW64_LOC environment variable is set. If this is not the case, the output will look as shown below:

... Looking for compiler 'MinGW64 Compiler (C)' ...

... Looking for environment variable 'MW_MINGW64_LOC' ...No.

The solution is to have an administrator set the environment variable. On Windows 10, you find this at:

Control Panel > System and Security > System > Advanced system settings > Environment Variables... > System variables: New...

The environment variable should have the following name and value:

Variable Name: MW_MINGW64_LOC

Variable Value: <SupportPackageRoot>\3P.instrset\mingw_w64.instrset

where <SupportPackage Root> is the output received for the command:

>> matlabshared.supportpkg.getSupportPackageRoot

Before the loop:

start_time = datetime('now');

rel_times = [];

In the loop, after

s = [s,h];

add

dt = milliseconds(datetime('now') - start_time);

rel_times = [rel_times, dt];

and change

plot(app.UIAxes,s);

to

plot(app.UIAxes, rel_times, s);

Note that performance of your application will get worse and worse over time, due to the way that you keep extending the s and rel_time arrays. Consider using a fixed-size buffer. Consider using animatedline() especially with a maximum number of points.

The Fixed-Point Tool collects ranges and proposes data types by data type groups rather than individual blocks.

A Data type group is a collection of block paths that need to have the same data type. If they don’t, Simulink will throw a data type mismatch error.

For instance, the output of the gain and the input to the subsystem need to have the same data type (they belong to the same data type group). Otherwise Simulink will throw a data type mismatch error. You can try this by changing the data type of the gain in the attached model to single.

For more details on the data type group, please refer to the documentation link below:

All the shared ranges are an aggregation of the data type group ranges. In this case, shared simulation range is the aggregate of the simulation ranges of the data type group and similarly the shared design range is an aggregate of the design ranges of the group.

Fixed Point Tool proposes a data type that can accommodate all these shared ranges for the group.You can infact click the link at the bottom of the Result Details pane to highlight all the members of the data type group.

The function which I am trying to fit my data to is defined as following:

VALUE=a1*x+a2*ln(y)+a3*abs(z)+a4

With: a1,a2,a3,a4 being the regression model's unknown coefficients

The following optimization constraints need to be imposed on the coefficients of the function:

a3<0; a4>0; a1*a2<0;

My initial guesses for the values of a1,a2,a3,a4 are defined as a row vector: [0,0,0,0].

The work flow which I would like to achieve should be similar to:

1. Define Function (referring to x,y,z data contained in columns of SampleData.csv file) :

function [ Value ] = FittingFunction(x,data)

Value=x(1).*data(:,2)+...

x(2).*log(data(:,3))+...

x(3).*abs(data(:,4))+...

x(4);

end

2.Define Initial Guess Row Vector x=[0,0,0,0]

Cost=sum((FittingFunction(x,data)-data(:,1)).^2);

5. Create a MultiStart Optimization Object and Start a pool of workers.

lowerBounds for MultiStart =[-Inf,-Inf,-Inf,-Inf]; upperBounds for MultiStart =[Inf,Inf,Inf,Inf];

Number of MultiStart Iterations= 5.

5. Run optimization in parallel.

delete(instrfind({'Port'},{app.PORT}))

app.x=serial(app.PORT,'BAUD',9600);

flushinput(app.x);

fopen(app.x); % otwarcie portu szeregowego

for i=1:app.Zakres.Value

drawnow

if app.Z == 1

break

end

h = str2num(fscanf(app.x));

app.aPolozenie.Value = h;

s = [s,h];

plot(app.UIAxes,s);

app.UIAxes.YLim = [ app.Od.Value app.Do.Value];

app.UIAxes.XLim = [0 app.Zakres.Value];

pause(.0001);

app.Zatrzymaj

end

end

This is my current status:

- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

Here is my current code (requires the data.mat file attached to my posting):

clc

clear variables

%%Loading Measurement Data File

load('data.mat')

%%Defining function to which the data will be fit to

% I would like to move away from using the anonymous function,

% and move to using the function file defined in the directory but I don't

% know how to pass a function file to the createOptimProblem function call.

fitfcn1 = @(x,data)x(1).*data.x+x(2).*log(data.y)+abs(data.z).*x(3)+x(4);

%%Defining lower and upper bounds for MultiStart procedure

lb1 = [-Inf,-Inf,-Inf,-Inf];

ub1 = [Inf,Inf,Inf,Inf];

%%Creating row vector for intial guess to optimization routine

p01 = 0*ones(1,4);

%%Defining optimization problem object

% Currently running lsqcurvefit, but I need to move to the genetic

% algorythm approach. I do not understand how to setup a genetic alorythm

% data fitting routine in matlab using the GA functionality built into the

% global optimization toolbox.

problem1 = createOptimProblem('lsqcurvefit','x0',p01,'objective',fitfcn1,...

'lb',lb1,'ub',ub1,'xdata',data,'ydata',data.value);

%%Creating live fitting progress plot

ms1 = MultiStart('PlotFcns',@gsplotbestf);

%%Running MultiStart Optimization Routine

[xmulti1,errormulti1] = run(ms1,problem1,5)

This is my current status:

- I am not sure if MultiStart or GlobalSearch can be setup to run with a Genetic Algorithm...

For example: A = ones(10,3); B = ones(10,3);

% inserting some NaNs and zeros A([1 6 10],1) = NaN; A([3 7],[2 3]) = NaN; B([3 7],2) = 0;

C = A + B;

I would like C = [1 2 2; 2 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2]

After installing the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package, the command

>> mex -setup

When I check the Add-On Explorer, or run the command

>> matlabshared.supportpkg.getInstalled

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

Running the 'mex -setup' command in verbose mode

>> mex -setup -v

... Looking for compiler 'MinGW64 Compiler (C)' ...

... Looking for environment variable 'MW_MINGW64_LOC' ...No.

rel_times = [rel_times, dt];

and change

plot(app.UIAxes,s);

to

plot(app.UIAxes, rel_times, s);

Note that performance of your application will get worse and worse over time, due to the way that you keep extending.

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

the MinGW Support Package is still shown as installed.

Why does MATLAB not detect the MinGW compiler anymore after a restart?

h = str2num(fscanf(app.x));

app.aPolozenie.Value = h;

s = [s,h];

plot(app.UIAxes,s);

app.UIAxes.YLim = [ app.Od.Value app.Do.Value];

app.UIAxes.XLim = [0 app.Zakres.Value];

pause(.0001);

app.Zatrzymaj

end

##### 1 Comment

Matt J
on 21 Feb 2020

The original question, before it was edited away by the OP, was how to "minimize" the code below (reproduced to the best of my recollection).

A=input('Enter Matrix A: ')

B=input('Enter Matrix B: ')

C=input('Enter Matrix C: ')

n=length(A);

Con=B;

Obs=C;

for x = 1: n-1

Con =[Con, A^(x)*B];

Obs =[Obs, C*A^(x)];

end

[a,b]=size(Con);

[c,d]=size(Obs);

if a==b

if det(Con)~=0

input('The systeme is controlable')

else

input('The systeme is not controlable')

end

elseif a~=b

if rank(Con)==n

input('The systeme is controlable')

else

input('The systeme is not controlable')

end

end

if c==d

if det(Obs)~=0

input('The systeme is observable')

else

input('The systeme is not observable')

end

elseif c~=d

if rank(Obs)==n

input('The systeme is observable')

else

input('The systeme is not observable')

end

end

### Answers (2)

Matt J
on 21 Feb 2020

Edited: Matt J
on 21 Feb 2020

A=input('Enter Matrix A: ')

B=input('Enter Matrix B: ')

C=input('Enter Matrix C: ')

n=length(A);

Con=cell(1,n); Obs=Con.';

Con{1}=B; Obs{1}=C;

for m = 2: n

Con{m} = A*Con{m-1};

Obs{m} = Obs{m-1}*A;

end

Con=cell2mat(Con); Obs=cell2mat(Obs);

analyzeIt(Con,"controllable",n)

analyzeIt(Obs,"observable",n)

function analyzeIt(Q,msg,n)

[a,b]=size(Q);

threshold=1e-8; %non-singularity tolerance

if (a==b && rcond(Q)>threshold) || (a~=b && rank(Q)==n)

disp("The system is "+msg)

else

disp("The system is not "+msg)

end

end

##### 0 Comments

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!