I need the values called u2(n) u3(n) u4(n) u5(n) to iterate with the four equations each time the loop runs. So it runs and say it gets u2=112 and u3=20, u4=20, u5=20 then I want it to use those values to run the equations again until the value of u5 is as close as possible to 250. The catch is that I need those values to start at 20 instead of nothing or zero which is causing me to get the error "Attempted to access u3(2); index out of bounds because nume1 (u3)=1." I know this has to be because I predefined those values as a scalar but I do not know any other way to do it. Thank you for your time.
clc; clear all n = 1; u1 = 500; r = .192; u6 = 20; h = 0; u5(n) = 20; u2(n) = 20; u3(n) = 20; u4(n) = 20;while h < 250 uc(n) = r*(u3(n) + u1) + (1 - 2*r)*(u2(n)); uv(n) = r*(u4(n) + u2(n)) + (1 - 2*r)*(u3(n)); ut(n) = r*(u5(n) + u3(n)) + (1 - 2*r)*(u4(n)); ui(n) = r*(u6 + u4(n)) + (1 - 2*r)*(u5(n)); u1 = 500; u2(n) = uc(n) u3(n) = uv(n) u4(n) = ut(n) u5(n) = ui(n) h = u5(n) u6 = 20; n = n+1;end
EDIT:
I fixed the code with the help of the comments below and was able to get it to work, it is a little slow and could be sped up but it works none the less. Here it is for future reference, maybe it will help someone else.
clc; clear all
n = 1; u1 = 500; r = .192; u6(n) = 20; u7(n) = 20; u8(n) = 20; u9 = 20; h = 0; u5(n) = 20; u2(n) = 20; u3(n) = 20; u4(n) = 20; g = 0;
while g < 250
uc(n) = r*(u3(n) + u1) + abs(1 - 2*r)*(u2(n)); uv(n) = r*(u4(n) + u2(n)) + abs(1 - 2*r)*(u3(n)); ut(n) = r*(u5(n) + u3(n)) + abs(1 - 2*r)*(u4(n)); ui(n) = r*(u6(n) + u4(n)) + abs(1 - 2*r)*(u5(n)); uk(n) = r*(u7(n) + u5(n)) + abs(1 - 2*r)*(u6(n)); ul(n) = r*(u8(n) + u6(n)) + abs(1 - 2*r)*(u7(n)); uy(n) = r*(u9 + u7(n)) + abs(1 - 2*r)*(u8(n)); u1 = 500; u2(n+1) = uc(n); u3(n+1) = uv(n); u4(n+1) = ut(n); u5(n+1) = ui(n); u6(n+1) = uk(n); u7(n+1) = ul(n); u8(n+1) = uy(n); u9 = 20; h = u5(n+1); n = n+1; g = h(end);
end
number_of_iterations = n-1;
t = (300/60)*number_of_iterations;
time = (0:5:575);
fprintf('Number of iterations to reach 250 degrees celcius is %.f\n',number_of_iterations) fprintf('Time it takes in minutes for x at 100 mm to reach 250 degrees celcius is %.f',t)
plot(time,u5)
xlabel('Time (Minutes)') ylabel('Iterations for U at 100 mm (Temperature)')
title('Temperature Vs. Time')
Best Answer