MATLAB: I need help storing values in an array from a for loop.

#forloop #array

n = input( 'How many values should be used to calculate the numerical solution?\n')
x= linspace(0, 2, n)
h = x(2)-x(1)
for i= 1:length(x)
y_a = (x.^2+1/2+1).^2;
y_euler = y +h*(1+4*x(i))*sqrt(y)
end
I need to store y_euler in an array of y values.

Best Answer

n = input( 'How many values should be used to calculate the numerical solution?\n')
x = linspace(0, 2, n)
h = x(2)-x(1)
for i= 1:length(x)
y_a(i) = (x(i).^2+1/2+1).^2;
y_euler(i,:) = y + h*(1+4*x(i))*sqrt(y);
end
However, you have not defined y and you do not use y_a in your calculations.
In my code, I assume that y is either scalar or a vector (row vector or column vector); my code would fail if y is 2 or more dimensional.