MATLAB: Error message that relates to output of user-defined function

erroroutputuserdefinedfuctions

Im trying to create a function that measures the displacement of a beam however I keep getting the same error message fo my code.
function y= displacement(x,a,W,E,I,L)
x=linspace(0,L)
if x<=a & x>=0
y= -((W*x.^2)/(6*E*I))*((3*a)-x)
else if x>=a & x<=L
y = -((W*a.^2)/(6*E*I))*((3*x)-a)
end
end
I=0.163
L=10
a=3
W=1000
y = displacement(x,a,W,E,I,L)
Error in displacement (line 2) x=linspace(0,L)
Output argument "y" (and maybe others) not assigned during call to "displacement".
And I'm really not sure why this occurs

Best Answer

One problem I see is that ‘x’ is an input argument to your function, then you overwrite it with a 100-element vector:
x=linspace(0,L)
The problem is that logical comparison statements are not straightforward with vectors.
I’m not certain what you want to do with your code. I will leave this to you to sort.