Vectors must be the same length Help

3 ビュー (過去 30 日間)
Bobby Punjabi
Bobby Punjabi 2017 年 9 月 22 日
コメント済み: Rena Berman 2017 年 9 月 28 日
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 件のコメント
Adam
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
Rena Berman 2017 年 9 月 28 日
(Answers Dev) Restored edit

サインインしてコメントする。

回答 (2 件)

KL
KL 2017 年 9 月 22 日
Your width variable has one more element than your d. Try
plot(width(2:end),d)
  2 件のコメント
Bobby Punjabi
Bobby Punjabi 2017 年 9 月 22 日
Its now showing no axis titles?
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
ylabel('Depth of Immersed Ship(m)')
xlabel('Width of Ship(m)')
title('Depth of immersed ship vs ship width')
plot(width(2:end),d)
KL
KL 2017 年 9 月 22 日
編集済み: KL 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.

サインインしてコメントする。


Guillaume
Guillaume 2017 年 9 月 22 日
編集済み: Guillaume 2017 年 9 月 22 日
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
Bobby Punjabi 2017 年 9 月 22 日
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);
this code does not work..
Guillaume
Guillaume 2017 年 9 月 22 日
編集済み: Guillaume 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);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by