How to define recursively symbolic function for differential equation system
Show older comments
Hi guys,
I have a system of differential equations and I am trying to solve it with dsolve. I would like to define symbolic function in a foor loop but I do not know how to do it.
Does anyone know a possible solution?
Cheers,
Alessio
2 Comments
Ameer Hamza
on 18 Nov 2020
What you describe seems possible, but you need to give more details for a specific answer.
T_K_86
on 18 Nov 2020
Answers (1)
Ameer Hamza
on 19 Nov 2020
This depends on what your ODE is. For example consider this ode
each different value of 'a' create a different ODE. Now consider, you want the 'a' to take values from set 0:0.01:1. You can solve the equation for all values of 'a' like this
syms y(t)
a = 0:0.01:1;
sol = cell(size(a));
for i = 1:numel(a)
eq = diff(y)==a(i)*y;
sol{i} = dsolve(eq);
end
In most cases, you can also solve the ODE for a general value of parameter 'a' and then later substitute the value from the vector
syms y(t) a
av = 0:0.01:1;
sol = dsolve(diff(y)==a*y);
sol_all = subs(sol, a, av)
11 Comments
T_K_86
on 19 Nov 2020
T_K_86
on 20 Nov 2020
T_K_86
on 20 Nov 2020
Ameer Hamza
on 20 Nov 2020
Instead of running for loop, you can directly create these symbolic functions using the following syntax
syms('A(t)', [10, 1])
T_K_86
on 23 Nov 2020
Ameer Hamza
on 24 Nov 2020
Which MATLAB release are you using?
T_K_86
on 26 Nov 2020
You cannot use syms to define an array of symbolic functions
syms('A(t)', [10, 1])
A(1)
Notice the effect is to create a single vector-valued function, and the (1) has the effect of evaluating the function with parameter 1, instead of indexing.
A = str2sym(compose("A%d(t)", 1:10))
B = A(1), B(7)
So that doesn't work either: you end up defining fancy variable names that just happen to look like functions.
So...
syms(compose('A%d(t)', 1:10))
A3(7)
But that doesn't give you vector or cell array of them.
I know I've managed to do it in the past, but it is not coming to mind at the moment. The forms of syms that permit you to have output do not handle functions, and sym() does not do it...
syms t
symfun('A9(t)', t)
A = cellfun(@(S) symfun(S, t), compose('A%d(t)', 1:10), 'uniform', 0)
A{3}(7)
Ah, that seems to have done it.
Ameer Hamza
on 26 Nov 2020
Yes, I think that the OP wanted to create symbolic variables like A1, A2, ..., A10. Although, the command
syms('A(t)', [10, 1])
creates a vector-symbolic function A, But it also creates symbolic functions A1(t), A2(t), ..., A10(t). The above command is equivalent to
syms(compose('A%d(t)', 1:10))
except the the first one also create A(t).
Walter Roberson
on 26 Nov 2020
The user wants to create functions dynamically, so A1(t), A2(t) ... A10(t) . But sym() cannot create symbolic functions, and cellfun(@syms) does not work because that happens to invoke syms in a mode that looks like it is accepting input and wanting output, and syms can only have output if it has no input...
syms() creates symbolic functions by invoking symfun() so we can cellfun() @symfun
syms(compose('A%d(t)', 1:10))
This is not equivalent to the cellfun I wrote. The cellfun I wrote creates the symbolic functions and gathers them into a cell array output, so you end up with A = {A1(t), A2(t), A3(t) ... A10(t)} which you would want in order to be able to process the functions afterwards.
Categories
Find more on Numeric Solvers 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!



