Clear Filters
Clear Filters

create vectorfield for an ODE

2 views (last 30 days)
Seb
Seb on 20 Apr 2011
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

Accepted Answer

Jarrod Rivituso
Jarrod Rivituso on 20 Apr 2011
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;
  1 Comment
Seb
Seb on 20 Apr 2011
thanks that sounds nice, i will try further on that.
PS: it was the ladder case :)

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!