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

조회 수: 1 (최근 30일)
CONNOR
CONNOR 2023년 11월 29일
댓글: CONNOR 2023년 11월 29일
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
CONNOR
CONNOR 2023년 11월 29일
There I think I did it and the error is at the bottom of the code
Dyuman Joshi
Dyuman Joshi 2023년 11월 29일
@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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 11월 29일
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
CONNOR
CONNOR 2023년 11월 29일
편집: CONNOR 2023년 11월 29일
so your saying where I have fun(grid(n,:)) it should say something like fun(d, t,
grid(n,:))?
CONNOR
CONNOR 2023년 11월 29일
Now its just saying Unrecocgnized function of variable 'd' or 't'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by