Can someone help me with this error message: Error using vertcat Dimensions of arrays being concatenated are not consistent. ? My code is attached below
1 view (last 30 days)
Show older comments
eul.m (source code)
function [t,y] = eul(fun,a,b,y0,N)
%Euler's method for solving the IVP
% y' = f(t,y) in [a,b], y(a) = y0
h = (b-a)/N;
t = zeros(N+1,1);
y = zeros(N+1,1);
t(1) = a;
y(1) = y0;
for i = 1:N
t(i+1) = t(i) + h;
y(i+1) = y(i) + h*fun(t(i),y(i));
end
----------------------------
exercise1 (driver file)
y0 = -2; % initial condition
a = 0; % starting time
b = 2; % ending time
N1 = 5; % number of subintervals, h = (b-a)/N
[t1,y1] = eul(fun,a,b,y0,N1);
N2 = 10;
[t2,y2] = eul(fun,a,b,y0,N2);
N3 = 20;
[t3,y3] = eul(fun,a,b,y0,N3);
%% Display Solution
exactY = @(t) -1./t;
Y = exactY(t);
disp('----------------------------------------------------');
disp('t_i True Euler 5 Euler 10 Euler 20')
disp('----------------------------------------------------')
formatSpec = '%.1f % .5f % .5f % .5f % .5f\n';
fprintf(formatSpec,[t1';Y';y1';y2(1:2:N2+1)'; y3(1:4:N3+1)']) // I think the problem is with this line but I don't know where.
The driver file runs when N1 =10, N2 = 20, and N3 = 40, but not the values I need. Any help is appreciated!
0 Comments
Answers (1)
Cris LaPierre
on 19 Feb 2021
Edited: Cris LaPierre
on 19 Feb 2021
This error occurs when you try to combine (stack) vectors with different number of columns.
a=1:3;
b=2:8;
c=[a;b]
Check that you have the same number of elements in each item you are trying to combine/stack.
[t1';Y';y1';y2(1:2:N2+1)'; y3(1:4:N3+1)']
Perhaps you meant to use commas instead of semicolons?
d=[a,b]
d = 1×10
1 2 3 2 3 4 5 6 7 8
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!