Changing the objective function (perform.Fcn) of neural network in MATLAB to customised one - matlab

I am trying to estimate the parameters using nueral networks with multiple layres for this equation;
y = x1*x2*(alpha1) + x3*(alpha2)
where x1, x2 and x3 are the inputs and y is the output. The neural network architecture is as follow, three inputs (x1,x2,x3) and two outputs (alpha1,alpha2) with multiple hidden layers.
The objective function will be as follow:
e = [y - (x1*x2*(alpha1) + x3*(alpha2))]
where y is the actual output and
(x1*x2*(alpha1) + x3*(alpha2))
is the estimated output, however, I can't change the perform.Fnc in matlab to customized one. I get the following error
[performFcn" must be '' or the name of a network performance function]
How can I solve this problem ? How can I use the estimated outputs from the NN as parameters inside the objective function
I tried to change the objective function by assigning the "net.perform.Fcn" to my customized function that I created, however, MATLAB didn't accept it and gave me this message error
"performFcn must be '' or the name of a network performance function"
How can I set my own objective function ?

Related

Transforming a transfer function into a differential equation in Matlab

I have the following code in matlab:
syms s
num = [2.4e8];
den = [1 72 90^2];
hs = poly2sym(num, s)/poly2sym(den, s);
hs
f = ilaplace(hs)
The inverse Laplace transform converts the transfer function in the "s" domain to the time domain.I want to know if there is a way to transform the s-domain equation to a differential equation with derivatives. The following figure is an example:
I'm trying to implement a dynamic system in an s-function and I need to put it in this format to be able to access the system states.
I tried to work with the state space of this system but it's giving an error that I couldn't solve, I even have an unanswered question here, about this: "S-Function error message: Output returned by S-function 'Hi_plant_sfcn' in 'untitled/S-Function' during flag=3 call must be a real vector of length 1".
If anyone knows how to do this, I appreciate it.

Warning " X is rank deficient to within machine precision" problem

I am trying to build a multiple linear regression in MATLAB with 20 predictors, which are categorical with 4 levels each. I am using the function "regress", like this (these are not the actual variables):
X = [ones(size(x1)) x1 x2 x3...x20];
[b,bint,r,rint] = regress(Y, X);
Before this, I transformed the vectors x1,x2...x20 in categorical variables with dummyvar.
I get this error and a lot of 0's in the b coefficients and this error:
Warning: X is rank deficient to within machine precision.
In the dummyvar documentation it is mentioned:
To use the dummy variables in a regression model, you must either delete a column (to create a reference group) or fit a regression model with no intercept term.
I tried not using the intercept ones(size(x1)) and I get the same error.
I would appreciate any input on how to solve this.
Try to simplify the problem down to the minimum working example, and then post that here, so we can reproduce it and help you through. See https://en.wikipedia.org/wiki/Rank_(linear_algebra)
for examples of rank deficiency.

resample() function in code generation. Any way to use variable Q downsampling factor?

I need to use resample() function to take a variable argument of Q downsampling factor in Simulink. Basically a Simulink fcn block containing this code:
function y = resample(data,Q)
y=resample(data,1000,Q);
On desktop simulation I can get variable Q to work as argument by specifying it as input to a MATLAB interpreted function, but since I need to generate a C code,my only option is to use the fcn block, obviously it won't compile due to above limitation.
error: the downsample factor Q must be constant
I understand this is a documented limitation of the resample function:
resample: The upsampling and downsampling factors must be specified as
constants. Expressions or variables are allowed if their values do not
change.
Any workaround or different approach to address this? Perhaps other block which is capable of doing the same job? ofc it has to be compatible with Simulink coder.
Thanks!
resample function would need to design filters and decide output sizes based on the sample factors. After code generation this cannot be changed, which is why this function needs the sampling factors to be constant.
But if the different downsampling factor values you need to support are limited you could use conditional branching with calls to resample in each branch with constant values. For example,
% Declare out as a var-size with max decided by the minimum downsampling factor
% Assuming data is [1000, 1]
coder.varsize('out', [500 1]);
out = zeros(500,1);
if Q == 2
out = resample(data,1000,2);
elseif Q == 4
out = resample(data,1000,4);
elseif ...
...
end
You also need to deal with variable sized data "out" in the rest of your MATLAB code and Simulink model if this is an output variable from MATLAB Function block.

Neural Network layer design

I am kind of new to neural network. This is one piece of code I've tried in Matlab
P= 0 + (rand(1) * 10);
T = (P-1)/(P+1);
net = newelm(P,T,5);
net = train(net,P,T);
Y = sim(net,P);
Now when I type net.B{1} and net.LW{1} in the command window of matlab, I get the bias weights and layer weights, but I also find that these weight values keep changing according to input values.
So can I have a predefined weight value, the one that doesn't change, for a particular function(and for any value of input), such that using these weight values I can design a neural network for a particular function. Like here I have T which is related to P by a particular equation.
If one of your inputs has a known relation to the output variable, take it out of the network instead of creating a complex workaround like fixing network weights. (It will be complex because of the variable interactions and nonlinear transformations inside the network.)
E.g.
Y = a*X1 + 3.6*X2 # relationship between Y and X2 is known
Then use neural network on this relation:
Y - 3.6*X2 = a*X1
^^^^^^^^^^ ^^^^
[target] [input]

matlab genetic algorithm solver complex input and output

My Matlab program has multiple inputs as a struct (in.a, in.b, etc.)
and multiple outputs (out.a, out.b, etc.)
I would like to use the genetic algorithm solver from teh optimization toolbox to find the best input in.a, while all the other inputs are constant. The fitness is one of the outputs, e.g. out.b(2,3).
How do I "tell" the solver this?
Thanks
Daniel
It is not uncommon in programming to have a situation where what is most convenient for your function and what some library call expects of it don't agree. The normal resolution to such a problem is to write a small layer in between that allows the two to talk; an interface.
From help ga:
X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the
FITNESSFCN using GA. [...] FITNESSFCN accepts a vector X of size
1-by-NVARS, and returns a scalar evaluated at X.
So, ga expects vector input, scalar output, whereas you have a structure going in and out. You would have to write the following (sub)function:
function Y = wrapper_Objfun(X, in)
in.a = X; %# variable being optimized
out = YOUR_REAL_FUNCTION(in); %# call to your actual function
Y = out.b(2,3); %# objective value
end
and then the call to ga will look like
X = ga(#(x) wrapper_Objfun(x,in), N);
where N is however large in.a should be.
Also have a read about it in Matlab's own documentation on the subject.