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
2021-3-21
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
2021-3-21
Sergey Kasyanov
2021-3-21
What are you need to do in global?
Mohammad Adeeb
2021-3-21
Mohammad Adeeb
2021-3-21
Sergey Kasyanov
2021-3-21
Can you write the equation?
Mohammad Adeeb
2021-3-21
编辑:Mohammad Adeeb
2021-3-21
Mohammad Adeeb
2021-3-21
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
2021-3-21
Walter Roberson
2021-3-21
Writing it that way is just a more compact way of writing it.
Sergey Kasyanov
2021-3-22
It is more readable.
类别
在 帮助中心 和 File Exchange 中查找有关 Communications Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!