MATLAB Answers

predictor corrector method error, what should i do?

1 view (last 30 days)
Mohammad Adeeb
Mohammad Adeeb on 25 Mar 2021
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 (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!