MATLAB: How to define a variable I just made with an IF statement errorvariable >> if x1 <= 0 x2 = x1./2 elseif x1 >= 0 x2 >= x1.*2 end x3 = sqrt(y.^2 + x2.^2); figure(1) plot(t,x3) Undefined function or variable 'x2' Best Answer It's a typo in the 4th line, remove the > and usex2 = x1.*2Note that if x1 is not <= 0, it must be > 0 , so you there is no need to test for >=0 in the elseif clause. So you can simply use else:if x1 <= 0 x2 = x1./2else x2 = x1.*2end Related SolutionsMATLAB: Place array in a matrix Perhaps I'm misunderstanding but if x = [1.0000 0 0.5000 1.0000 0.5000 1.5000 0 -1.0000 -1.0000 1.0000 0.5000 -1.0000];which is a 6x2 array, then wouldn't x*x'be the desired 6x6 array? MATLAB: Not getting the right output for the if statement? [piecewise function] First off, I doubt your code runs since else if is not valid syntax. Second, I am pretty sure that 1 <= x < 2 is not doing what you think it is doing. Related QuestionMultiple <,=,> in an if statement‘Matrix dimensions must agree error’How to solve this without goto statement
Best Answer