11%% example_pass_extra.m
22% IVP Solver Toolbox
33%
4- % Example of passing extra parameters to a function defining an IVP .
4+ % Example of passing extra parameters to functions .
55%
66% Copyright © 2021 Tamas Kis
7- % Last Update: 2022-06-05
7+ % Last Update: 2022-06-06
88% Website: https://tamaskis.github.io
99% Contact: tamas.a.kis@outlook.com
1010
1717
1818
1919
20- %% SOLUTION
20+ %% EXAMPLE #1
2121
22- % parameters
23- b = 5 ; % damping constant [N.s/m]
24- k = 1 ; % spring constant [N/m]
25- m = 2 ; % mass [kg]
26- x0 = 1 ; % initial position [m]
27- dx0 = 0 ; % initial velocity [m/s]
22+ % function definition
23+ a = 5 ; b = 5 ; c = 5 ; d = 5 ;
24+ f = @(x ,y ) - a *(x - b )^2 - c *(y - d )^2 ;
2825
29- % forcing function
30- F = @(t ) cos(pi * t );
26+ % call function, update "a", then call function again
27+ f(2 ,2 )
28+ a = 20 ;
29+ f(2 ,2 )
3130
32- % initial condition
33- y0 = [x0 ;
34- dx0 ];
31+ % NOTE: Both evaluations of "f" yield the same result.
3532
36- % differential equation
37- f = @(t ,y ) [y(2 );
38- -(b / m )*y(2 )-(k / m )*y(1 )+(1 / m )*F(t )];
3933
4034
35+ %% EXAMPLE #2
4136
42- %% ALTERNATE SOLUTION
37+ % original function definition
38+ a = 5 ; b = 5 ; c = 5 ; d = 5 ;
39+ f = @(x ,y ) - a *(x - b )^2 - c *(y - d )^2 ;
40+ f(2 ,2 )
4341
44- % parameters
45- b = 5 ; % damping constant [N.s/m]
46- k = 1 ; % spring constant [N/m]
47- m = 2 ; % mass [kg]
48- x0 = 1 ; % initial position [m]
49- dx0 = 0 ; % initial velocity [m/s]
42+ % update values of constants
43+ a = 10 ; b = 10 ; c = 10 ; d = 10 ;
44+ f = @(x ,y ) - a *(x - b )^2 - c *(y - d )^2 ;
45+ f(2 ,2 )
5046
51- % forcing function
52- F = @(t ) cos(pi * t );
47+ % NOTE: The two evaluations of "f" no longer yield the same result.
5348
54- % initial condition
55- y0 = [x0 ;
56- dx0 ];
5749
58- % assigns function handle to differential equation
59- f = @(t ,y ) f_extra(t ,y ,b ,k ,m ,F );
6050
61- % defines differential equation
62- function dy = f_extra(t ,y ,b ,k ,m ,F )
51+ %% EXAMPLE #3
6352
64- % unpacks state vector
65- x = y(1 );
66- xdot = y(2 );
67-
68- % preallocates state vector derivative
69- dy = zeros(size(y ));
70-
71- % assembles state vector derivative
72- dy(1 ) = xdot ;
73- dy(2 ) = -(b / m )*xdot -(k / m )*x +(1 / m )*F(t );
74-
75- end
53+ % define function where constants can vary as well
54+ f_extra = @(x ,y ,a ,b ,c ,d ) - a *(x - b )^2 - c *(y - d )^2 ;
55+
56+ % define original function by assigning function handle to f_extra
57+ f = @(x ,y ) f_extra(x ,y ,5 ,5 ,5 ,5 );
58+ f(2 ,2 )
59+
60+ % update values of constants
61+ f = @(x ,y ) f_extra(x ,y ,10 ,10 ,10 ,10 );
62+ f(2 ,2 )
63+
64+ % NOTE: This example is an alternate solution to Example #2.
65+
66+
67+
68+ %% EXAMPLE #4
69+
70+ % sets values of constants
71+ a = 10 ; b = 10 ; c = 10 ; d = 10 ;
72+ f = @(x ,y ) f_extra2(x ,y ,a ,b ,c ,d );
73+ f(2 ,2 )
74+
75+ % MATLAB function must be declared at end
76+ function f = f_extra2(x ,y ,a ,b ,c ,d )
77+ f = - a *(x - b )^2 - c *(y - d )^2 ;
78+ end
79+
80+ % NOTE: This example is an alternate solution to the second part of
81+ % Example #2.
0 commit comments