ode23 and ode45 problem
7 次查看(过去 30 天)
显示 更早的评论
Suppose we have a differential equation dy/dx=-2x+4y^2 over the range x=0 to 1 with y(0)=0. I need to solve this question with 'ode23' and ode45 in matlab. Does anybody help me?
2 个评论
John D'Errico
2019-5-26
Next time, why not make an effort to do your homework yourself? Then show what you tried and ask for someone to help fix it, if you do not succeed.
采纳的回答
Stephan
2019-5-26
% Solve symbolic (blue line in plot)
syms y(x) x
eqn = diff(y,x) == -2*x+4*y^2
sol_symbolic = dsolve(eqn,y(0)==0);
fplot(sol_symbolic,[0 1])
hold on
% solve numeric with ode45 (red dots in plot)
[V, S] = odeToVectorField(eqn);
fun = matlabFunction(V,'vars', {'x','Y'});
[x, sol_numeric] = ode45(fun,[0 1], 0);
plot(x, sol_numeric,'or')
hold off
With this example code it should be possible to use ode23 also
4 个评论
Jan
2019-5-27
Almost nice.
function main
[y,x] = ode45(@H, [0,1], [0,1]);
% ^ ^ round parentheses
end
function xl = H(x,y)
xl = zeros(2,1);
xl(1) = x(2);
xl(2) = -2 * x + 4 * y^2;
end
Use @H instead of defining the function to be integrated as char 'H'. The latter is still working, but outdate for 15 years now.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!