Vectors must be the same length Help
Afficher commentaires plus anciens
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 commentaires
John D'Errico
le 24 Sep 2017
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
le 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
le 28 Sep 2017
(Answers Dev) Restored edit
Réponses (2)
KL
le 22 Sep 2017
Your width variable has one more element than your d. Try
plot(width(2:end),d)
2 commentaires
Bobby Punjabi
le 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.
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 commentaires
Bobby Punjabi
le 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);
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!