how can i solve this error , predictor corrector method
Ältere Kommentare anzeigen
hey all
im trying to solve 2nd ode with RK4 and predictor corrector method , and i;m still receving this error ,what should i do?
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:3
K11 = fn(x(i), yic(:, i));
K12 = fn(x(i) + h/2, yic(:, i) + h*K11/2);
K21 = fn(x(i) + h/2, yic(:, i) + h*K12/2);
K22 = fn(x(i) + h, yic(:, i) + h*K21);
yic(:,i+1) = yic(:, i) + h/6*(K11 + 2*K12 + 2*K21 + K22);
end
%%predictor corrector equation defenition
for i = 5:11
y_star=yic(:,i) + (h/24)*((55*fn(x(i),yic(:,i)))-(59*fn(x(i-1),yic(:,i-1))) +(37*fn(x(i-2),yic(:,i-2))) - (9*fn(x(i-3),yic(:,i-3))));
yic(:,i+1) = yic(:,i)+(h/24)*((9*fn(x(i),y_star(:,i)))+(19*fn(x(i),yic(:,i)))-(5*fn(x(i-1),yic(:,i-1)))+(fn(x(i-3),yic(:,i-3))));
end
%% print the output in the form of table
out=[x' y_exact' yic(1,:)'];
fprintf(' ------------------------------------------------\n')
fprintf('| X | Y(exact) | Y(approximate)) |\n')
fprintf(' ------------------------------------------------\n')
fprintf('| %3.1f | %3.6f | %3.6f |\n',out')
fprintf(' ------------------------------------------------\n')
%% plotting the grapgh
plot(x, yic(1,:), 'r*');
hold on;
plot(x,y_exact,'b.-')
xlabel('X axis')
ylabel('Y axis')
legend('Y_e_x_a_c_t','Y_a_p_p')
%% defining the function according to the proplem
function dy = fn(~, y)
dy = [0, 1
2, -1] * y; %change the matrix due to your intital conditins
%and the equation in your proplem
end
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 30)
yic(:,i+1) = yic(:, i) + h/6*(K11 + 2*K12 + 2*K21 + K22);
>>
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Get Started with Aerospace Blockset finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
