Vectors must be the same length Help

7 Ansichten (letzte 30 Tage)
Bobby Punjabi
Bobby Punjabi am 22 Sep. 2017
Kommentiert: Rena Berman am 28 Sep. 2017
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 Kommentare
Adam
Adam am 25 Sep. 2017
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 am 28 Sep. 2017
(Answers Dev) Restored edit

Melden Sie sich an, um zu kommentieren.

Antworten (2)

KL
KL am 22 Sep. 2017
Your width variable has one more element than your d. Try
plot(width(2:end),d)
  2 Kommentare
Bobby Punjabi
Bobby Punjabi am 22 Sep. 2017
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 am 22 Sep. 2017
Bearbeitet: KL am 22 Sep. 2017
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.

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 22 Sep. 2017
Bearbeitet: Guillaume am 22 Sep. 2017
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 Kommentare
Bobby Punjabi
Bobby Punjabi am 22 Sep. 2017
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 am 22 Sep. 2017
Bearbeitet: Guillaume am 22 Sep. 2017
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by