How to use fitrgp in Simulink?
6 views (last 30 days)
Show older comments
Can anyone tell me how to use fitrgp command in MATLAB Simulink?
i tried the following code in simulink within MATLAB function block but i'm receiving error.
if true
% code
myinput = [rand(100,1) rand(100,1)];
myoutput= rand(100,1);
coder.extrinsic('fitrgp');
% opts = statset('fitrgp');
% opts.TolFun = 1e-2;
sigma0 = 1e-5;
sigmaF0 = 1e-5;
d = 2;
sigmaM0 = 1e-2*ones(d,1);
% GPMdl = rand(100,1);
GPMdl = fitrgp(myinput,myoutput,'Basis','constant','FitMethod','exact',...
'PredictMethod','exact','KernelFunction','ardsquaredexponential',...
'Optimizer','QuasiNewton',...
'KernelParameters',[sigmaM0;sigmaF0],'Sigma',sigma0,'Standardize',1);
end
Error message is '' Function output 'GPMdl' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type.''
i tried to define the GPMdl initially like
if true
% code
myinput = [rand(100,1) rand(100,1)];
myoutput= rand(100,1);
coder.extrinsic('iddata');
GPMdl=iddata(myinput,myoutput,1);
coder.extrinsic('fitrgp');
% opts = statset('fitrgp');
% opts.TolFun = 1e-2;
sigma0 = 1e-5;
sigmaF0 = 1e-5;
d = 2;
sigmaM0 = 1e-2*ones(d,1);
% GPMdl = rand(100,1);
GPMdl = fitrgp(myinput,myoutput,'Basis','constant','FitMethod','exact',...
'PredictMethod','exact','KernelFunction','ardsquaredexponential',...
'Optimizer','QuasiNewton',...
'KernelParameters',[sigmaM0;sigmaF0],'Sigma',sigma0,'Standardize',1);
end
but still i receive the above error.
when i predefine GPMdl like GPMdl = rand(100,1) which is wrong because fitrgp output is 1x1 RegressionGP class / metaclass.
if true
% code
myinput = [rand(100,1) rand(100,1)];
myoutput= rand(100,1);
coder.extrinsic('fitrgp');
% opts = statset('fitrgp');
% opts.TolFun = 1e-2;
sigma0 = 1e-5;
sigmaF0 = 1e-5;
d = 2;
sigmaM0 = 1e-2*ones(d,1);
GPMdl = rand(100,1);
GPMdl = fitrgp(myinput,myoutput,'Basis','constant','FitMethod','exact',...
'PredictMethod','exact','KernelFunction','ardsquaredexponential',...
'Optimizer','QuasiNewton',...
'KernelParameters',[sigmaM0;sigmaF0],'Sigma',sigma0,'Standardize',1);
end
then i receive the following error
MATLAB expression 'fitrgp' is not numeric.
Block GPMdl (#1160) While executing: State During Action
thanks in advance!
0 Comments
Answers (1)
Ayush Aniket
on 13 Mar 2025
Edited: Ayush Aniket
on 13 Mar 2025
When using MATLAB functions like fitrgp within a MATLAB Function block in Simulink, you must handle the fact that Simulink does not natively support MATLAB objects like RegressionGP. Instead, you need to use "coder.extrinsic" to declare the function as extrinsic, meaning it will be executed in the MATLAB workspace (which you have done). However, the MATLAB Function block cannot directly handle non-numeric data types returned by extrinsic functions.
Since the output GPMdl is a MATLAB object, you cannot use it directly in Simulink. Instead, you can encapsulate the entire process of fitting the Gaussian Process Regression model and extracting any necessary numeric data for use in Simulink into a custom MATLAB function. This function can then be called as an extrinsic function from within the MATLAB Function block in Simulink. Refer the code snippet below:
function predictedOutput = fitAndExtractGPR()
% Example inputs
myinput = [rand(100,1) rand(100,1)];
myoutput = rand(100,1);
% Parameters for fitrgp
sigma0 = 1e-5;
sigmaF0 = 1e-5;
d = 2;
sigmaM0 = 1e-2 * ones(d, 1);
% Fit the Gaussian Process Regression model
GPMdl = fitrgp(myinput, myoutput, 'Basis', 'constant', 'FitMethod', 'exact', ...
'PredictMethod', 'exact', 'KernelFunction', 'ardsquaredexponential', ...
'Optimizer', 'QuasiNewton', ...
'KernelParameters', [sigmaM0; sigmaF0], 'Sigma', sigma0, 'Standardize', 1);
% Extract some numeric data from the model
% For example, extract the predicted responses for a new set of inputs
newInput = [rand(10,1) rand(10,1)];
predictedOutput = predict(GPMdl, newInput);
end
In your Simulink model, use the MATLAB Function block with the following code:
function y = fitGPRModel(~)
%#codegen
% Declare the custom function as extrinsic
coder.extrinsic('fitAndExtractGPR');
% Initialize output
y = zeros(10, 1); % Adjust size based on what fitAndExtractGPR returns
% Call the custom function
predictedOutput = fitAndExtractGPR();
% Assign the output
y = predictedOutput;
end
This could be one way to achieve your objectives.
0 Comments
See Also
Categories
Find more on Gaussian Process Regression 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!