create vectorfield for an ODE
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone, my problem is the following:
consider the differential equation x' = f(x), where f is a vector function. consider that for my problem, i have a formula to create f. given a vector a of appropriate size, f( x ) = a* (a_transpose*x). Now i want to test this differential equation with variation of a. Instead of writing f by hand, i want to have one function that has the vector a as an input and gives me the function handle of the right hand side of the ode.
But i came a cross a hard problem: you cant change a handle once its done. also, i cant write the right hand side first and declare the anonymous function because one cant just write x(1), x(2) ...
is there a easy way to figure this out?
thanks a lot
0 个评论
采纳的回答
Jarrod Rivituso
2011-4-20
Are you looking to have "a" change within a single call to ODE45, or would you like it to change on each subsequent call?
If the latter is the case, you could use an anonymous function wrapper to the derivative function
function dy = derivs(t,y,a)
dy = a*y;
And then
>> derivWrapper = @(t,y) derivs(t,y,5);
>> [t,y] = ode45(derivWrapper,[0 10],2)
>> plot(t,y)
If the former is the case, you could modify your derivative function to change "a" as you want to.
function dy = derivs(t,y)
if (t > 5)
a = 1;
else
a = 3;
end
dy = a*y;
更多回答(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!