Clear Filters
Clear Filters

I don't know how to solve the error it is producing

1 view (last 30 days)
P = 10; % kips
E = 30000; % ksi
H = 10; % feet
rho = 0.28; % lbm/in^3
db = [1.0, 6.0]; % inches - values of lower and upper bounds for db
tb = [0.01, 0.5]; % inches - values of lower and upper bounds for tb
numIters = 3;
segments = 15;
fun = @(b, t) cost(db, tb, P, E, H, rho, numIters, segments);
bounds = [db; tb];
Nvals = [10000];
for Ns=1:length(Nvals)
[Jmin, xopt] = randsearch(fun, bounds, Ns);
end
Unable to perform assignment because the left and right sides have a different number of elements.

Error in solution>randsearch (line 58)
Jout(n) = fun(grid(n,:));
%% Functions
function [eigenVal, eigenVec] = PowerMeth(A, numIter)
eigenVec = ones(size(A, 1),1);
for i=1:numIter
b = A*eigenVec;
eigenVal = norm(b);
eigenVec = b/eigenVal;
end
end
function Pcr = get_Pcr(ni, nn, E, d, t, H)
segments = nn-1;
h = H/segments;
A = full(spdiags([-1.0/h^2*ones(nn-2, 1), 2.0/h^2*ones(nn-2, 1), -1.0/h^2*ones(nn-2, 1)], [-1, 0, 1], nn-2, nn-2));
[eigenVal, eigenVec] = PowerMeth(inv(A), ni);
I = (pi.*((d+(2.*t)).^4-d.^4))/64;
Pcr = 1.0/eigenVal*E*I;
Ptrue = (pi^2*E*I)/(H^2);
if ((Ptrue-Pcr)/Ptrue)>0.01
disp('Cant compute correctly')
end
end
function C = cost(d, t, P, E, H, rho, numIter, segments)
M = (pi.*t.*H.*rho).*(t+d);
Pcr = get_Pcr(numIter, segments, E, d, t, H);
if Pcr > P
C = (2.*M)+(t.^-2);
else
C = Inf;
end
end
function [Jmin, xopt] = randsearch(fun, bounds, Ns)
% Dimension of problem
nvar = length(bounds);
grid = zeros([Ns, nvar]);
Jout = zeros(Ns,1);
for k=1:nvar
low = bounds(k,1);
high = bounds(k,2);
grid(:,k) = low + (high-low).*rand(Ns,1);
end
for n=1:Ns
Jout(n) = fun(grid(n,:));
end
% Find index of max
[Jmin, idx] = min(Jout);
xopt = grid(idx,:);
end
%% THIS IS THE ERROR
% Unable to perform assignment because the left and right sides have a
% different number of elements.
%
% Error in HW6b>randsearch (line 69)
% Jout(n) = fun(grid(n,:));
%
% Error in HW6b (line 20)
% [Jmin, xopt] = randsearch(fun, bounds, Ns);
  5 Comments
CONNOR
CONNOR on 29 Nov 2023
There I think I did it and the error is at the bottom of the code
Dyuman Joshi
Dyuman Joshi on 29 Nov 2023
@CONNOR, That's a fair assumption.
However, when we run the code provided, we might encounter some other or additional errors compared to error the poster gets. Sometimes there is some missing information, such as data files not available, variables not defined, etc etc.
Thus, to clarify, we ask the poster to copy and paste the full error message. And the error message also provides valuable information which aids in solving the issue, as the error message obtained here is not guaranteed to be exactly the same as the one encountered by the poster.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2023
fun = @(b, t) cost(db, tb, P, E, H, rho, numIters, segments);
fun is a function that expects two parameters, and ignores both of them to call a function with constant inputs.
for n=1:Ns
Jout(n) = fun(grid(n,:));
end
you are calling fun() with a single parameter that is a vector, rather than with two parameters.
But remember fun() ignores its inputs, so it does not matter what the parameter is.
What is important is that fun() is returning a non-scalar result.
  2 Comments
CONNOR
CONNOR on 29 Nov 2023
Edited: CONNOR on 29 Nov 2023
so your saying where I have fun(grid(n,:)) it should say something like fun(d, t,
grid(n,:))?
CONNOR
CONNOR on 29 Nov 2023
Now its just saying Unrecocgnized function of variable 'd' or 't'

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!