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);
''

Antworten (1)

Sergey Kasyanov
Sergey Kasyanov am 21 Mär. 2021

1 Stimme

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 Kommentare

Mohammad Adeeb
Mohammad Adeeb am 21 Mär. 2021
i want the code to take those values of [dy , dy2] and then get thme in the for loop
Sergey Kasyanov
Sergey Kasyanov am 21 Mär. 2021
What are you need to do in global?
Mohammad Adeeb
Mohammad Adeeb am 21 Mär. 2021
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 am 21 Mär. 2021
the code run , but the value of y is only matrix 2*2 , and its should be 11*2
Sergey Kasyanov
Sergey Kasyanov am 21 Mär. 2021
Can you write the equation?
Mohammad Adeeb
Mohammad Adeeb am 21 Mär. 2021
Bearbeitet: Mohammad Adeeb am 21 Mär. 2021
d2y/dx2 +dy/dx-2y=0
but i should use my own function not a built in one
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));
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 am 21 Mär. 2021
thank u so much ; but i have qustion why do you use dy as matrix and multiply it by y
Walter Roberson
Walter Roberson am 21 Mär. 2021
Writing it that way is just a more compact way of writing it.
Sergey Kasyanov
Sergey Kasyanov am 22 Mär. 2021
It is more readable.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Communications Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 21 Mär. 2021

Kommentiert:

am 22 Mär. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by