Stuck on printing two different vectors, maybe Syntax?

>> 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.

回答(1 个)

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.

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by