Why am I getting an error that matrices must agree?
Show older comments
Working on this question about a Fence problem(Have to write a script about it) and here is my script:
prompt= 'State the Area: ';
Area = input(prompt,'s');
prompt= 'State the Straight Cost: ';
StrCo = input(prompt,'s');
prompt= 'State the Curved Cost: ';
CurCo = input(prompt,'s');
R = .01:.01:sqrt(2*Area/pi);
L = (Area - 1/2*pi*R.^2)/(2*R);
Cost = (2*L+ 2*R)*StrCo +pi*R*CurCo;
[BestCost,Index] = min(Cost);
BestR = R(Index);
BestL = L(Index);
disp(['The best cost is',num2str(min(Cost)),'.'])
disp(['The best radius is',num2str(BestR),'.'])
disp(['The best length is',num2str(BestL),'.'])
But when I go and run my script, I am so confused on why I get this error:
>> untitled3
State the Area: 1600
State the Straight Cost: 30
State the Curved Cost: 40
Error using -
Matrix dimensions must agree.
Error in untitled3 (line 10)
L = (Area - 1/2*pi*R.^2)/(2*R);
1 Comment
Stephen23
on 28 Feb 2018
See also original question:
Accepted Answer
More Answers (2)
L = (Area - 1/2*pi*R.^2)./(2*R);
Best wishes
Torsten.
Andrei Bobrov
on 28 Feb 2018
prompt= 'State the Area: ';
Area = input(prompt);
prompt= 'State the Straight Cost: ';
StrCo = input(prompt);
prompt= 'State the Curved Cost: ';
CurCo = input(prompt);
R = .01:.01:sqrt(2*Area/pi);
L = (Area - 1/2*pi*R.^2)./(2*R);
Cost = (2*L+ 2*R)*StrCo +pi*R*CurCo;
[BestCost,Index] = min(Cost);
BestR = R(Index);
BestL = L(Index);
disp(['The best cost is ',num2str(min(Cost)),'.'])
disp(['The best radius is ',num2str(BestR),'.'])
disp(['The best length is ',num2str(BestL),'.'])
Categories
Find more on Programming 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!