predictor corrector method error, what should i do?

2 views (last 30 days)
hey all;
im trying to solve second order ODE using RK4 and predictor-correctoe method without using any built in mtlab function
here is my code :
clear all;
close all;
clc;
h=0.1; %step size (changable according to the proplem)
x=0:h:1; %the X domain range
yic = [[1;-2],zeros(2, length(x)-1)]; %intial condition in form of matrix
%(changable according to the proplem)
%*********************************************
% Exact solution
%*********************************************
y_exact=exp(-2*x); %define your equation for exact solution
%**********************************************
%********* Numerical solution *****************
%% RK4th order definition
for i = 1:6
if i<=3
K1 = fn(x(i), yic(:, i));
K2 = fn(x(i) + h/2, yic(:, i) + h*K1/2);
K3 = fn(x(i) + h/2, yic(:, i) + h*K2/2);
K4 = fn(x(i) + h, yic(:, i) + h*K3);
yic(:, i+1) = yic(:, i) + h/6*(K1 + 2*K2 + 2*K3 + K4);
else if i>=4
y_star = yic(:,i)+((h/24).*(55.*fn(x(i),yic(i))-(59.*fn(x(i),yic(:,i-1)))+(37.*fn(x(i),yic(i-2)))-(9.*fn(x(i),yic(i-3)))));
y6 = yic(:,i) + ((h/24).*(9.*fn(x(i+1),y_star(1,:)) +(19.*fn(x(i),yic(:,i)))+(5.*fn(x(i-1),yic(:,i-1))) +(fn(x(i-2) , yic(:,i-2)))));
end
end
end
%% defining the function according to the proplem
function dy = fn(x, y)
dy = [0, 1
2, -1] .* y; %change the matrix due to your intital conditins
%and the equation in your proplem
end
here is my error
Unable to perform assignment because the size of the left side is 2-by-1 and the size of the right side is 2-by-2.
Error in HW3_multistep_method (line 32)
yic(:, i+1) = yic(:, i) + h/6*(K1 + 2*K2 + 2*K3 + K4);
what should i do ?
help me pls

Answers (1)

sanidhyak
sanidhyak on 25 Jun 2025
Edited: sanidhyak on 25 Jun 2025
I understand that you are facing some issues while solving a second-order ODE using "RK4" and the "predictor-corrector" method in MATLAB. Upon reviewing the code and the error message I found out that the root cause of this issue lies in the definition of your "fn" function. More specifically, the operation used inside the function is "element-wise multiplication (.*)" between a 2×2 matrix and a 2×1 vector, which leads to a size mismatch. MATLAB expects regular "matrix multiplication (*)" in this case.
To resolve this, please update the "fn" function as shown below:
function dy = fn(x, y)
A = [0, 1;
2, -1];
dy = A * y; % Correct matrix multiplication
end
This change ensures that your derivative calculations match the mathematical formulation of your system of ODEs and are compatible with the 2×1 y vector.
Also, kindly make sure all other references to "yic(i)" in your "predictor-corrector" steps are updated to "yic(:, i)" to maintain proper vector indexing. For example,
Before:
fn(x(i), yic(i))
After:
fn(x(i), yic(:, i))
These corrections should completely eliminate the dimension mismatch errors and allow your code to run perfectly.
For further reference, kindly refer to the following official documentation:
I hope this helps!
  2 Comments
Walter Roberson
Walter Roberson on 25 Jun 2025
More specifically, the operation used inside the function is "element-wise multiplication (.*)" between a 2×2 matrix and a 2×1 vector, which leads to a size mismatch.
For clarity: Since roughly R2015b, MATLAB has had "implicit expansion" that automatically "expands" dimensions of size 1 for binary operators to match the size of the other array. So 2x2 matrix .* 2x1 matrix would be treated similarly to 2x2 matrix .* (repmat, 2x1 matrix, 1, 2) giving a 2 x 2 result. So the 2x2 .* 2x1 giving 2x2 result itself is considered valid.
The challenge is that the assignment is to a 2 x 1 output slot, and a 2 x 2 matrix does not fit into a 2 x 1 output slot.
The real solution in this particular case is as noted, using the * operator instead of the .* operator. But in some cases, the real solution would be to provide a 2 x 2 output area instead of a 2 x 1 output area.

Sign in to comment.

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!