How to define recursively symbolic function for differential equation system

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

What you describe seems possible, but you need to give more details for a specific answer.
Ok Ameer,
let's make an example..Let's imagine I have a system of differential equation where the function to be solved are I1,I2,I3.....In where n varies from 1 to N (let's say 100). How can I define these symbolic function without doing it manually 100 times?
Cheers,
Alessio

Sign in to comment.

Answers (1)

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

Thank you, but I was referring to solve a set of differential equations in matrix form... so I need to set a vector I=[I1(t); I2(t); I3(t);....; In(t)] and each element is a symfun.. there is any way to do it?
To be more clearer:
clear all; close all; clc;
N=4;
for i=1:N
syms(sprintf('z%d(t)', i));
end
In this case I generate four symbolic function that will be used in a system of differential equation like: A*z'+B*z=C (where A and B are matrix and C is a vector). To use dsolve I need to create the vector z that is z=[z1;z2;z3;z4]...How can I do it?
Cheers
clear all; close all; clc;
for i =1:4
syms(sprintf('A%d(t)',i))
end
A_tot=[A1;A2;A3;A4]
I can do it manually for 4 function..but I cannot do it in a four loop..
Instead of running for loop, you can directly create these symbolic functions using the following syntax
syms('A(t)', [10, 1])
sorry man..i got this error message:
Error using syms (line 222)
Invalid variable name.
Error in Untitled (line 1)
syms('A(t)', [10, 1])
R2018a.... is this the problem? Which release would you suggest me?
You cannot use syms to define an array of symbolic functions
syms('A(t)', [10, 1])
A(1)
ans = 
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))
A = 
B = A(1), B(7)
B = 
Index exceeds the number of array elements (1).

Error in sym/subsref (line 902)
R_tilde = builtin('subsref',L_tilde,Idx);
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)
ans = 
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)
ans(t) = 
A = cellfun(@(S) symfun(S, t), compose('A%d(t)', 1:10), 'uniform', 0)
A = 1x10 cell array
{1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun} {1×1 symfun}
A{3}(7)
ans = 
Ah, that seems to have done it.
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).
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.

Sign in to comment.

Tags

Asked:

on 18 Nov 2020

Commented:

on 26 Nov 2020

Community Treasure Hunt

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

Start Hunting!