how can i solve this error , predictor corrector method
4 次查看(过去 30 天)
显示 更早的评论
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);
>>
0 个评论
采纳的回答
Walter Roberson
2021-3-28
No, that error does not show up, but you had some other errors. I marked the changes with HERE
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))+(19*fn(x(i),yic(:,i)))-(5*fn(x(i-1),yic(:,i-1)))+(fn(x(i-3),yic(:,i-3)))); %HERE
end
%% print the output in the form of table
out=[x' y_exact' yic(1,1:end-1)']; %HERE
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,1:end-1), 'r*'); %HERE
hold on;
plot(x,y_exact,'b.-')
xlabel('X axis')
ylabel('Y axis')
legend('Y_a_p_p','Y_e_x_a_c_t') %HERE
%% 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
3 个评论
Walter Roberson
2021-3-28
I ran the code in R2020b (same version you indicate you are using) and my output is the same as what I posted above. The output I posted above has been run directly on R2021a on the Mathworks servers.
Your error is occuring on line 30 of your code according to the error message, but in the code you posted, that line is at most line 24, and is on line 18 of the code I posted. So you are not running the same code.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!