why do i receive this error

x=0.1;
x= 0:0.1:1;
yic =[1 -2] ' ;
for i=1:(length(x)-1)
K11 = fn (x, yic);
K21 = fn (x , yic);
K12 = fn ((x + h) , (yic + (h*K11)) , (yic + (h*K21)));
K22 = fn ((x + h),(yic + h*K11) , (yic + h*K21));
y1 = ( yic + 0.5*h*(K11 + K12 ));
y2 = ( yic + 0.5*h*(K12 + K22));
end
function f = fn( x , yic )
dy = yic(2);
dy2 = 2*yic(1)-yic(2);
end
when i run the code this error appear :
''
Output argument "f" (and maybe others) not assigned during call to "HW2>fn".
Error in HW2 (line 22)
K11 = fn (x, yic);
''

回答 (1 件)

Sergey Kasyanov
Sergey Kasyanov 2021 年 3 月 21 日

1 投票

Hello!
You don't define f in fn function.
Are you want to return f = [fy, fy2]? In that case:
function f = fn( x , yic )
dy = yic(2);
dy2 = 2*yic(1)-yic(2);
f = [dy, dy2];
end
Also you have an error in another lines. Maybe you should to correct it in that way:
K12 = fn ((x + h) , [(yic + (h*K11)) , (yic + (h*K21))] );
K22 = fn ((x + h), [(yic + h*K11) , (yic + h*K21)] );

11 件のコメント

Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 21 日
i want the code to take those values of [dy , dy2] and then get thme in the for loop
Sergey Kasyanov
Sergey Kasyanov 2021 年 3 月 21 日
What are you need to do in global?
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 21 日
im trying to write a code to solve 2nd order ode by using reduction of order and runge-kutta 4th order method
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 21 日
the code run , but the value of y is only matrix 2*2 , and its should be 11*2
Sergey Kasyanov
Sergey Kasyanov 2021 年 3 月 21 日
Can you write the equation?
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 21 日
編集済み: Mohammad Adeeb 2021 年 3 月 21 日
d2y/dx2 +dy/dx-2y=0
but i should use my own function not a built in one
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 21 日
i did the following changes to my code
clear all;
close all;
clc;
h=0.1; %step size
x=0:h:1; %the X domain range
yic = [1 -2]'; %intial condition
t=exp(-2*x);
for i=1:(length(x)-1)
K11(i) = fn (x(i), yic(i));
K21(i) = fn (x(i) , yic(i));
K12(i) = fn ((x(i) + h) , [(yic(i) + (h*K11(i))) , (yic(i) + (h*K21(i)))] );
K22(i) = fn ((x(i) + h), [(yic(i) + h*K11(i)) , (yic(i) + h*K21(i))] );
y1(i+1) = ( yic(i) + 0.5*h*(K11(i) + K12(i) ));
y2(i+1) = ( yic(i) + 0.5*h*(K12(i) + K22(i)));
end
plot(x,y1,'k*')
hold on;
plot(x,y_exact,'r.-')
xlabel('X axis')
ylabel('Y axis')
legend('Y_e_x_a_c_t','Y_a_p_p')
function f = fn( x , yic )
dy = yic(2);
dy2 = 2*yic(1)-yic(2);
end
.
but i recive the following error :
Index exceeds the number of array elements (1).
Error in HW2>fn (line 41)
dy = yic(2);
Error in HW2 (line 25)
K11(i) = fn (x(i), yic(i));
Sergey Kasyanov
Sergey Kasyanov 2021 年 3 月 21 日
h = 0.1;
x = 0:h:1;
y = [[1;-2],zeros(2, length(x)-1)];
for i = 1:length(x)-1
K1 = f(x(i), y(:, i));
K2 = f(x(i) + h/2, y(:, i) + h*K1/2);
K3 = f(x(i) + h/2, y(:, i) + h*K2/2);
K4 = f(x(i) + h, y(:, i) + h*K3);
y(:, i+1) = y(:, i) + h/6*(K1 + 2*K2 + 2*K3 + K4);
end
function dy = f(x, y)
dy = [0, 1
2, -1] * y;
end
plot(x, y(1,:), 'k*');
Mohammad Adeeb
Mohammad Adeeb 2021 年 3 月 21 日
thank u so much ; but i have qustion why do you use dy as matrix and multiply it by y
Walter Roberson
Walter Roberson 2021 年 3 月 21 日
Writing it that way is just a more compact way of writing it.
Sergey Kasyanov
Sergey Kasyanov 2021 年 3 月 22 日
It is more readable.

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

カテゴリ

ヘルプ センター および File ExchangeCommunications Toolbox についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 3 月 21 日

コメント済み:

2021 年 3 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by