how can i solve this error , predictor corrector method

4 ビュー (過去 30 日間)
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 28 日
コメント済み: Mohammad Adeeb 2021 年 3 月 28 日
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);
>>

採用された回答

Walter Roberson
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')
| X | Y(exact) | Y(approximate)) |
fprintf(' ------------------------------------------------\n')
------------------------------------------------
fprintf('| %3.1f | %3.6f | %3.6f |\n',out')
| 0.0 | 1.000000 | 1.000000 | | 0.1 | 0.818731 | 0.818733 | | 0.2 | 0.670320 | 0.670324 | | 0.3 | 0.548812 | 0.548817 | | 0.4 | 0.449329 | 0.000000 | | 0.5 | 0.367879 | 0.006703 | | 0.6 | 0.301194 | 0.008704 | | 0.7 | 0.246597 | -0.000656 | | 0.8 | 0.201897 | -0.000329 | | 0.9 | 0.165299 | -0.000159 | | 1.0 | 0.135335 | -0.000266 |
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
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.
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 28 日
No it’s the same , but i put it in a different m file and it’s worked , thanks alot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by