# Conditional for function

Hi, I have a conditional inside the for loop, the conditional changes the function in each loop, but first i get the error “qxloc is not defined” so add qxloc (x) = nothing;

but this does not solve the problem and the conditional does not work.

``````for e = 1:3
#x1 = xnod[LaG[e,NL1], X];  x2 = xnod[LaG[e,NL2], X]
#y1 = xnod[LaG[e,NL1], Y];  y2 = xnod[LaG[e,NL2], Y]
#L =  hypot(x2-x1, y2-y1)

ang1 = atan(2,4);

if e == 1
qxloc(x) =-2.8*sin(ang1)*cos(ang1)
qyloc(x) =-2.8*cos(ang1)^2

elseif e == 2

qxloc(x) = -2*cos(ang1)^2
qyloc(x) = 0

elseif e == 3

qxloc(x) = 0
qyloc(x) = 0

end

end
``````

How can I solve it?

where did you add it? You should add it outside the loop (or simply `local gxloc`).

can you provide a minimal example of the code and the error?

You should probably use, for example, `qxloc = x -> cos(x) ` to assign the name qxloc to each of the possible function.

1 Like

Hi, It is a for loop with 3 different elements, in each element the qxloc and qyloc function changes because it is used in a BVProblem function for different conditions.

the function where change this parameter (qxloc, qyloc) is:

``````
function diffeq1!(dydx,y,x,p)

#            y[v_,:]          = v
dydx[v_]   = y[t_]          # = theta
dydx[t_]   = y[M_]/(E*I)    # = M/(EI)
dydx[M_]   = y[V_]          # = V
dydx[V_]   = qyloc(x)       # = qyloc
dydx[u_]   = y[fax_]/(A*E)  # = u
dydx[fax_] = -qxloc(x)       # = faxial

end``````

qxloc = x → cos (x) has solved my problem, thank you very much

``````qxloc =   [ x -> -2.8*sin(ang1)*cos(ang1)

x -> 0

x -> 0]

qyloc =   [ x -> -2.8*cos(ang1)^2

x -> 0

x -> 0]
``````