Stuck on printing two different vectors, maybe Syntax?

1 view (last 30 days)
>> N = 1062;
tFinal = 5;
h=tFinal/N;
t=linspace(0,tFinal,N+1);
z=zeros(1,N+1);
z(1)=1;
for n=1:N
z(n+1) = z(n) + h* ((10*(t(n)+1)^8/((t(n)+1)^10.+9)));
end
for n=1:N
x(n+1) = x(n) + h * ((8*(x(n))/(t(n)+1))-(t(n)+1)*(x(n)^2));
end
plot(t,x,t,z,'--')
xlabel('t'); ylabel('y'); title('Graph');
error100= max(abs(x-xExact));
fprintf('The maximum error for N = %.1f is = %0.2f.\n',N, error100)
Error using plot
Vectors must be the same length.

Answers (1)

Walter Roberson
Walter Roberson on 26 Feb 2021
Edited: Walter Roberson on 26 Feb 2021
x(n+1) = x(n) + h * ((8*(x(n))/(t(n)+1))-(t(n)+1)*(x(n)^2));
That uses x(n) before x has been assigned to at all.
error100= max(abs(x-xExact));
xExact has not been assigned to. Based upon context one might expect z to have been named ?
Your error message could have been caused by x already existing in a larger size at the time you run the code.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!