I made a function file but it is not readig in my other file. I need help having it read and possibly with my for loop also.

for my function I used
function vp=velocity( u,L,y,h,P,p,V) vp=V.*(y/h)+((h.^2)/(2*u)).*((p-P)/L).*(((y/h).^2)-(y/h));
and saved it as velocity.
Next I plugged it into the following code. clear all; u=1.5*10^-3; h=0.05; L=100; p=3.5*10^5; V=15; P=[3.46*10^5,3.48*10^5,3.50*10^5,3.52*10^5,3.54*10^5]; y=0:0.005:0.05; a=1; for a=1:5 [v]= velocity(u,h,L,p,V,P,y); if a==1 A(1:25)=v; elseif a==2 B(1:25)=v; elseif a==3 C(1:25)=v; elseif a==4 D(1:25)=v; else a==5 E(1:25)=v; end end plot(A,y,'r') hold on plot(B,y,'k') hold on plot(C,y,'g') hold on plot(D,y,'y') hold on plot(E,y,'b') title('Velocity') xlabel('') ylabel('Height')
However when I do this the following errors appear. ??? Error using ==> plus Matrix dimensions must agree.
Error in ==> velocity at 7 vp=V.*(y/h)+((h.^2)/(2*u)).*((p-P)/L).*(((y/h).^2)-(y/h));
Error in ==> problem2 at 11 [v]= velocity(u,h,L,p,V,P,y);
Can someone please help me?!

1 Kommentar

It would be helpful if you used the markup tools to format your code into a more readable form.

Antworten (3)

There is quite a bit that seems syntactically wrong with this piece of code, but what is causing your specific error seems to be that when you calculate "vp" inside the function, your two terms are vectors of different length. "V" is 1x11, and "p" is 1x5.
Do you know how to add breakpoints to the code? You can halt the code execution to see what you variables look like when you hit that line.
You should go back and format your question with the code format button!
If we re-write your velocity function as:
function vp=velocity( u,L,y,h,P,p,V)
N = V.*(y/h);
M = ((h.^2)/(2*u)).*((p-P)/L).*(((y/h).^2)-(y/h));
size(N)
size(M)
vp=N+M;
Then we can see the problem. N is 1-by-11 and M is 1-by-5. You cannot add these two vectors.
Your matrix dimensions are inconsistent for V and p in the function VELOCITY.
One thing that I notice off the bat - the input arguments that you enter to call the function VELOCITY must be in the same order as they appear in the function file.
Also, you can set a breakpoint in your code to drill into it deeper. Check out the documentation on this:

Diese Frage ist geschlossen.

Gefragt:

am 14 Mär. 2011

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by