I am trying to plot a graph for a second order ODE for the midpoint method.

My error is: Unable to perform assignment because the left and right sides have a different number of elements.
Error in Question3A (line 8)
x(2) = -5*heaviside(t-6)*exp((1/2)*(6-t))-4*A-8*B;
Error in Q3script (line 29)
A(:,i+1)= A(i) + (dt*Question3A(thalf,Ahalf,B(i)));
A = zeros(2,length(t1));
B = zeros(2,length(t1));
%Initial conditions
A(1) = 2;
B(1) = 3;
% Write a loop to solve the ODE using the midpoint method
for i = 1:length(t1)-1
thalf = 0.5*(t1(i) + t1(i+1));
Ahalf = A(i) + (0.5*dt*Question3A(t1(i),A(i),B(i)));
A(:,i+1)= A(i) + (dt*Question3A(thalf,Ahalf,B(i)));
end
my function file
function x = Question3A(t,A,B)
x = zeros(2,1);
x(1) = B;
x(2) = -5*heaviside(t-6)*exp((1/2)*(6-t))-4*A-8*B;

 Respuesta aceptada

Jan
Jan el 5 de Nov. de 2021
Editada: Jan el 7 de Nov. de 2021
t1 = 1:10; % A bold guess
dt = t1(2) - t1(1);
A = zeros(2, length(t1));
B = zeros(2, length(t1));
Now A and B have 2 rows.
%Initial conditions
A(1) = 2;
B(1) = 3;
This defines the initial value of the first row and first columns of A and B.
for i = 1:length(t1)-1
thalf = 0.5*(t1(i) + t1(i+1)); % thalf: [1x1]
Ahalf = A(i) + (0.5*dt*Question3A(t1(i),A(i),B(i))); % Ahalf: [2x1] !!!
A(:,i+1)= A(i) + (dt*Question3A(thalf,Ahalf,B(i))); % Crash
end
Unable to perform assignment because the left and right sides have a different number of elements.

Error in solution>Question3A (line 21)
x(2) = -5*heaviside(t-6)*exp((1/2)*(6-t))-4*A-8*B;
function x = Question3A(t,A,B)
x = zeros(2,1);
x(1) = B;
% A is [2x1], B is [1x1], the complete expression is [2x1]
% and cannot be assigned to the scalar x(2)
x(2) = -5 * heaviside(t-6) * exp((1/2) * (6-t)) - 4 * A - 8 * B;
end
Use the debugger to examine such problems:
dbstop if error
Now Matlab stops at the error and you can check the sizes of the used variables.
I assume, you do not want A and B as [2 x numel(t)] matrices, but as vectors. It is more compact to use one matrix instead:
Y = zeros(2, length(t1));
Y(1, 1) = 2;
Y(2, 1) = 3;
for i = 1:length(t1)-1
thalf = 0.5 * (t1(i) + t1(i+1));
Yhalf = Y(:, i) + 0.5 * dt * Question3A(t1(i), Y(1, i), Y(2, i));
Y(:, i+1) = Y(:, i) + dt * Question3A(thalf, Yhalf(1), Yhalf(2));
end
% [EDITED] YHalf -> Yhalf.

4 comentarios

t1 is actually: t1 = 0:dt:10
where dt=0.1
I dont know if this changes what you have said?
As long as t1 and dt is defined in a meaningful way, my arguments are matching.
Habiba Kelani
Habiba Kelani el 7 de Nov. de 2021
Editada: Jan el 7 de Nov. de 2021
do you know why 'Yhalf' says unregnizable. ive had this issue and it wont go away @Jan
Jan
Jan el 7 de Nov. de 2021
Editada: Jan el 7 de Nov. de 2021
@Habiba Kelani: Yes, this is a typo. I've written YHalf one line above and Yhalf with a lower case h later. I've fixed it now.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 5 de Nov. de 2021

Editada:

Jan
el 7 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by