Vectors must be the same length Help
이전 댓글 표시
Hi Guys,
I keep getting the error that "Vectors must be the same length." I desperately need to fix this somehow.
i = 1;
width = 0.1;
depth = 0;
d= 0;
while width < 20
a(i)=sqrt(1+400/(width(i).^2));
d(i)=(8.050 * (width(i).^2)/40 * (acosh(a(i))+a(i) * sinh(acosh(a(i)))) * 0.01 *3 * sqrt(5)/(width(i))).^(2/3);
width(i+1)= width(i) + 0.1;
depth = d(i);
i = i +1;
end
plot(w,d)
Thanks
댓글 수: 3
John D'Errico
2017년 9월 24일
Was it really necessary to remove your question by putting garbage text in there? This insults the person who answered your question. It hurts anyone else who might have benefited from the answers.
Adam
2017년 9월 25일
One advantage of leaving these questions edited away at least is that we can clearly see not to waste our time in future if the same person asks a new question.
Rena Berman
2017년 9월 28일
(Answers Dev) Restored edit
답변 (2개)
KL
2017년 9월 22일
Your width variable has one more element than your d. Try
plot(width(2:end),d)
댓글 수: 2
Bobby Punjabi
2017년 9월 22일
Come on!! plot first and then add the labels.
plot(width(2:end),d)
ylabel('Depth of Immersed Ship(m)')
xlabel('Width of Ship(m)')
title('Depth of immersed ship vs ship width')
But then again, I haven't taken time to improve your program. I have only fixed your error. As Guillaume suggests you can improve and simplify your code to a larger extent. Read the documentation and understand how things work.
I desperately need to fix this somehow
Well, the proper way to fix this is to actually look at what your code is doing by using the debugger and stepping through your program. Or looking at what it is producing. You should have seen straight away that your width has one more element than your d.
This comes down from the fact that you're using a while loop where you're creating width for the next step. A for loop would have been a lot less complicated:
width = 0.1:0.1:20
for i = 1:numel(width)
a(i) = sqrt(1+400/(width(i).^2));
d(i)=(8.050 * (width(i).^2)/40 * (acosh(a(i))+a(i) * sinh(acosh(a(i)))) * 0.01 *3 * sqrt(5)/(width(i))).^(2/3);
end
But even simpler would have been not to use a loop at all:
width = 0.1:0.1:20;
a = sqrt(1 + 400./width.^2);
depth = (8.050 * width.^2/40 .* (acosh(a) + a.*sinh(acosh(a))) * 0.01 * 3 .* sqrt(5)./width) .^ (2/3);
plot(width, depth);
To finish, I think you need to be more careful about your use of spaces in your expressions. You wrote
d(i) = ... acosh(a)+a * sinh ...
the spacing would imply that this would be evaluated as
d(i) = ... (acosh(a)+a) * sinh
wheres it'll be evaluated as
d(i) = ... acos(a) + a*sinh
댓글 수: 2
Bobby Punjabi
2017년 9월 22일
this code does not work..
Saw that you used dotted .^ (which you didn't need in your original code) but missed that you didn't use dotted ./ and dotted .*. Fixed now:
width = 0.1:0.1:20;
a = sqrt(1 + 400./width.^2);
depth = (8.050 * width.^2/40 .* (acosh(a) + a.*sinh(acosh(a))) * 0.01 * 3 .* sqrt(5)./width) .^ (2/3);
plot(width, depth);
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!