Function handle too literal

3 次查看(过去 30 天)
Cole Butler
Cole Butler 2018-5-18
I'm trying to define a new function, let's call it "h", for later use with the MATLAB function ode45. As a bit of a preface, I am trying to look at the solutions of linear map in the plane. To begin, based on what 2x2 matrix the user inputs, there are two equations (for each row of the matrix), we will call f1 and f2. When we wish to define these using a function handle for h, we want to replace the standard x and y denotation with Y(1) and Y(2) (our choice of letter here is arbitrary). We do this as follows:
F1 = subs(f1, {'x', 'y'}, {'Y(1)','Y(2)'});
F2 = subs(f2, {'x', 'y'}, {'Y(1)','Y(2)'});
Now, we wish to define h using these two new functions, now that the functions have a usable form (they are defined in terms of Y(1) and Y(2)).
h = @(t,Y) [F1; F2];
However, this is literally how h is defined. Thus, when we want to see solutions represented by the two functions, say with the argument
[t,ya] = ode45(h, [0 1.5], [0 1])
we get an error. How do I explicitly define h in terms of the functions F1 and F2, so, for example, I can expect
>>h =
@(t,Y) [ 2*Y(1); 4*Y(1) + 2*Y(2)]
rather than
>> h =
@(t,Y) [F1; F2]

回答(1 个)

Walter Roberson
Walter Roberson 2018-5-18
You should be using either matlabFunction() or odeFunction() to construct the function handles for numeric use.
  2 个评论
Cole Butler
Cole Butler 2018-5-18
I'm not sure I know what you mean.
Walter Roberson
Walter Roberson 2018-5-18
h = matlabFunction( [f1; f2], 'vars', {'t', [x; y]});
Notice no construction of F1 and F2 or 'Y(1)' or 'Y(2)'. The vector of variables [x;y] will be merged together into a single parameter by indexing the input by rows (because of the ";"), and the first parameter will be t.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by