Invalid setting for parameter 'Gain' - simulink

I am trying to create a simple controller, but keep receiving an error for the gain block stating that there is an undefined variable 'u'. I do not understand Simulink well and I am just trying to replicate an old homework problem right now. The code I have here was provided as a solution, but I still receive the error when I try to run it. Any insights as to what might be going on?
I= 10; Wl= 5; k= 2; J= 1;
%set initial conditions
thetaIC= 0; phiIC= 0; x0= zeros(4,1);
%fix theta= 0, check output
[xe, ue]= trim('Ex3_System',x0,0,x0,1)
[A,B,~,~]= linmod('Ex3_System', xe, ue)
%choose your desired poles
p= linspace(-1.2,-1.5,4)
%recall the minus sign
K= -acker(A,B,p)
%perturb initial condition
thetaIC= deg2rad(5);
sim('Ex3_Controller');
Invalid setting in 'Ex3_Controller/Gain' for parameter 'Gain'. Caused
by:
Error using hw12 (line 57)
Error evaluating parameter 'Gain' in 'Ex3_Controller/Gain'
Error using hw12 (line 57)
Undefined function or variable 'u'.
Error using hw12 (line 57)
Variable 'u' does not exist.
Suggested Actions:
• Load a file into 'Base Workspace'. - Fix
• Create a new variable. - Fix
Update: After removing the u term from the gain block, I received a different error:
Error using hw12 (line 57)
Error in port widths or dimensions. Output port 1 of 'Ex3_Controller/Gain' is a one dimensional vector with 4 elements.
Error using hw12 (line 57)
Error in port widths or dimensions. Input port 1 of 'Ex3_Controller/Model1' is a one dimensional vector with 1 elements.

The Gain block takes the value of the input signal and multiplies it by the value of the gain. In your case the gain is K and that is all you need to put into the gain block (i.e. remove the *u, Simulink handles that for you.)
Once that is done, the dimension error you are getting is because your controller requires u to be a scalar, but you are feeding a 4 element vector into it. You need to change the appropriate parameter of the Gain block so that it does a matrix multiplication, taking the 4-by-1 matrix K and (matrix) multiplying it with the 4 element "out" signal to produce a scalar.

Related

Receiving unknown errors when trying to use fsolve to find the solutions of a function

I have to plot the solution for masses between 4 and 8kg, which I wrote using the code below:
weight=linspace(4,8,100)
Then I use a for loop to find the solutions for the weights within this vector, while also declaring the variable a, and a guess of the time required:
for z=1:length(weight)
a=(weight./(1360*pi)).^(1/3)
tguess=14400
timeinseconds=fsolve(#(t)cookingtimes(t,a),tguess)
timeinhours=timeinseconds/(60*60)
end
The function that I mentioned previously is below:
function F=cookingtimes(t,a)
k=1:22;
alpha=0.7*10^(-7);
F(1)=(2./(pi*0.464))*sum(((-1).^(k-1))./k).*sin(k*pi*0.464).*exp(((-(k.^2)).*(pi.^2).*((alpha.*t))./(a.^2)))-57/80
end
Where F(1) represents the following equation 1, which will be equal to 57/80. Rather than adding until infinity, I chose the value of 22 as the equation converges to this value. r/a is equal to 0.464, which is why this is present within my function.
When running this code, I am given errors that I cant manage to solve.
Arrays have incompatible sizes for this operation.
Error in Homework1>cookingtimes (line 58)
F(1)=(2./(pi*0.464))*sum(((-1).^(k-1))./k).*sin(k*pi*0.464).*exp(((-(k.^2)).*(pi.^2).*.((alpha.*t))./(a.^2)))-57/80
Error in Homework1 (line 28)
timeinseconds=fsolve(#(t)cookingtimes(t,a),tguess)
Error in fsolve (line 264)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
The arrays that I have produced are all of the same length, which is why I am very stuck on this issue.
You are trying to perform an operation on two arrays that have different sizes. In your case, the issue may be caused by the fact that you are defining the variable a as a vector inside the for loop, but using it in the function cookingtimes as a scalar. To fix this, you can modify your function to accept a vector of a values and return a corresponding vector of function values.
I modified the cookingtimes function to accept a vector of a values, and return a corresponding vector of function values.
function F=cookingtimes(t,a)
k=1:22;
alpha=0.7*10^(-7);
F=(2./(pi*0.464))*sum(((-1).^(k-1))./k).*sin(k*pi*0.464).*exp(((-(k.^2)).* (pi.^2).*((alpha.*t))./(a.^2)))-57/80;
end
In the for loop, You need to compute a for each weight value, and use a vector of a values in the call to fsolve. Also store the solutions in a vector timeinseconds instead of a scalar, as there are multiple solutions to be found.
for z=1:length(weight)
a=(weight(z)./(1360*pi)).^(1/3); % compute a for each weight value
tguess=14400;
timeinseconds(z)=fsolve(#(t)cookingtimes(t,a),tguess);
timeinhours=timeinseconds/(60*60);
end

Evaluating an expression using MATLAB

I am trying to evaluate a parameter using Matlab , here is the code
miu_not=1.257*1e-6; % permeability of free space
efslon_not=8.854*1e-12;% permittivity of free space
efslon_rg=input('Enter the relative permittivity of ground ')
segma_g=input('Enter the ground conductivity') ;
b=input('enter the conductor radius');
f= input('Enter the frequency');
w=2*pi*f;
prop_ground=sqrt(1i*w*miu_not(segma_g+(1i*w*efslon_not*efslon_rg)))
It is giving me an error regarding the indices for the last line that it must be positive or logic , while i don't have any array in the equation ???
The exact error message is:
Array indices must be positive integers or logical values.
Error in Untitled (line 8)
prop_ground=sqrt(1i*w*miu_not(segma_g+(1i*w*efslon_not*efslon_rg)))
In your last statement
prop_ground=sqrt(1i*w*miu_not(segma_g+(1i*w*efslon_not*efslon_rg)))
is there supposed to be an operator between the miu_not and segma_g?
I think it's evaluating to miu_not(%number%) so Matlab thinks you're trying to index miu_not by number.

Matlab - MultiStart gets stuck using fmincon

I am currently trying to run a minimum-distance estimation of a dynamic search model with the MultiStart routine and fmincon and a bounded parameter space. My problem right now is that the routine gets stuck at a random point of the estimation, i.e. at some point one of the iterations gets stuck in an infinite loop or so and nothing moves forward.
My problem structure is the following:
problem = createOptimProblem('fmincon',...
'objective',#(x)objective(x,fixedInput),...
'x0',x_0,...
'lb',[0 0 0 0 0.001 0], 'ub',[50 1500 30 79 1 1],...
optimoptions(#fmincon,'Algorithm','interior-point','Display','off','UseParallel',true,));
ms = MultiStart('UseParallel',true,'Display','iter','StartPointsToRun','bounds');
The objective function has as output the weighted distance between supplied empirical moments and parametric counterparts of the dynamic model that I aim to fit to the data.
I run into the same problem no matter which algorithm I choose for the fmincon problems (though the process gets stuck much later when I use active-set instead of interior-point or sqp). I know that MultiStart has the MaxTime option, but this is not convenient in my case as I want to run the estimation on a large set of starting values (~2000) and the whole estimation stops too early then.
Does anyone know about a way to circumvent this problem?
Thanks a lot in advance!
Best,
banan
EDIT:
I get the following output when interrupting the routine:
Operation terminated by user during search(line 110)
In objective(line 42)
s_par = search(x, fixed);
In parallel_function>#(x)objective(x,fixedInput)
In finDiffEvalAndChkErr
In parfinitedifferences
In parallel_function (line 478)
consume(base, limit, F(base, limit, supply(base, limit)));
In parfinitedifferences
In nlconst (line 347)
parfinitedifferences(XOUT,lb,ub,f, ...
In fmincon (line 750)
nlconst(funfcn,X,l,u,full(A),B,full(Aeq),Beq,confcn,options,defaultopt, ...
In fmultistart
In fmultistart
In parallel_function>make_general_channel/channel_general (line 914)
O = F(C{:});
In remoteParallelFunction (line 38)
out = parallel.internal.pool.serialize(feval(channel, channelArgs{:}));
Operation terminated by user during distcomp.remoteparfor/getCompleteIntervals (line 127)
In parallel_function>distributed_execution (line 820)
[tags, out] = P.getCompleteIntervals(chunkSize);
In parallel_function (line 587)
R = distributed_execution(...
In fmultistart
In MultiStart/run (line 268)
fmultistart(problem,startPointSets,msoptions);
In MinDist_HM (line 42)
[theta_est,fval,exitflag,outp,manymins] = run(ms,problem,2000)

MATLAB: Using FZERO on a function which has a vector output

I am working on my thesis and running in some programming problems in Matlab. I am trying to implement the ''golden Bisection Method'' to speed up my code. To this end, I've consulted the build in function FZERO.
So I am determining the difference between two vectors which are both (1x20).
Difference = Clmax_dist-cl_vec;
Clmax_dist comes from a semi-empirical method and cl_vec comes from the excecution of an external AVL.exe file.
Essentially, this difference depends only on one single variable AOA because the Clmax_dist vector is a constant. Hence, I am constantly feeding a new AOA value to the AVL.exe to obtain a new cl_vec and compare this again to the constant Clmax_dist.
I am iterating this until one of the element in the vector becomes either zero or negative. My loop stops and reveals the final AOA. This is a time consuming method and I wanted to use FZERO to speed this up.
However, the FZERO documentation reveals that it only works on function which has a scalar as input. Hence, my question is: How can I use FZERO with a function which has a vector as an output. Or do i need to do something totally different?
I've tried the following:
[Difference] = obj.DATCOMSPANLOADING(AOA);
fun=#obj.DATCOMSPANLOADING;
AOA_init = [1 20];
AOA_root = fzero(fun,AOA_init,'iter');
this gave me the following error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 423)
while fb ~= 0 && a ~= b
Error in CleanCLmax/run (line 11)
AOA_root = fzero(fun,AOA_init,'iter');
Error in InitiatorController/moduleRunner (line 11)
ModuleHandle.run;
Error in InitiatorController/runModule (line 95)
obj.moduleRunner(ModuleHandle);
Error in RunSteps (line 7)
C.runModule('CleanCLmax');
The DATCOMSPANDLOADING function contains the following:
function [Difference] = DATCOMSPANLOADING(obj,AOA)
[Input]= obj.CLmaxInput; % Creates Input structure and airfoil list
obj.writeAirfoils(Input); % Creates airfoil coordinate files in AVL directory
[Clmax_dist,YClmax,Cla_mainsections] = obj.Clmax_spanwise(Input); % Creates spanwise section CLmax with ESDU method
[CLa] = obj.WingLiftCurveSlope(Input,Cla_mainsections); % Wing lift curve slope
[Yle_wing,cl_vec] = obj.AVLspanloading(Input,CLa,AOA); % Creates spanloading with AVL
Difference = Clmax_dist-cl_vec;
end
If I need to elaborate further, feel free to ask. And of course, Thank you very much.
fzero indeed only works on scalars. However, you can turn your criterion into a scalar: You are interested in AOA where any of the elements in the vector becomes zero, in which case you rewrite your objective function to return two output arguments: minDifference, which is min(Difference), and Difference. The first output, minDifference is the minimum of the difference, i.e. what fzero should try to optimize (from your question, I'm assuming all values start positive). The second output you'd use to inspect your difference vector in the end.

Simulink Matlab Function Block 'Size mismatch' Error

I am using a Matlab function block in a Simulink model. The Input is a 1x32 binary stream and the Output y is an integer between 0 and 15. I have 2 variables defined within this function called chip=zeros(17,32) and reconst_op=zeros(1,32). My Problem is that when I compare these two variables in an if condition as follows:
function y = De_Spreading_Mod(input)
if all(reconst_op==chip(1,:)) .. do something..
OR 2. if all(reconst_op(1,:)==chip(1,:))
Both the above conditions cause Matlab throw an error as follows:
for case 1, Size mismatch (size [:? x 32] ~= size [1 x 32]). Mismatched varying and fixed sizes indicate a probable run-time error. If this diagnostic is incorrect, use indexing to explicitly make the varying size fixed.
for case 2, MATLAB Function Interface Error: Simulation stopped due to out of bounds error. Runtime Error: Attempted to access 1 element of data reconst_chip_op whose runtime size is 0.
However, if I run the same Piece of code in a separate m file, it works as expected but only in the Simulink embedded matlab function block, it is an issue.
Any help would be appreciated