user-defined function as input in "dsolve"

2 views (last 30 days)
Hello everyone, I am new with MATLAB so maybe I can do the following and still haven't figured out how.
I want to use
dsolve ('Dz=a*z+b*t+c(z,t)','t')
where a and/or b are symbolic and
c(z,t) is a user-defined function?
The specific form of the expression is only an example (in principle it could/should be more general. The important thing here is the use of a user-defined function within the expression that defines the differential equation to be solved.
Is there any way I ca do something to this effect?

Accepted Answer

Miltos
Miltos on 8 Jul 2011
I think I found the way - it was hidden in an example about using dsolve for a system of ODE that are extracted from a matrix!!
Anyway...
So suppose you have a defined a function r(x,y) (either directly with inline or anonymous function, or extracting from a solution of a previous system, matrix etc)
Then as I understand it you create the string of it
eq=char(r(x,y))
[note if r(x,y) corresponds to a solution ans.w of a previous system then one can define the string eq diectly as eq=char(ans.w)]
and then you use dsolve as follows
dsolve(strcat('Dx=',eq),'t')
[where t is the independent variable (which could be y)]
stracat(s1,s2) is a built-in function that "keeps together" the two strings (in general more than two) 'Dx=' and eq creating a new string that corresponds to the symbolic expression implied by Dx=r(x,y).
It has worked for few examples I played with, but maybe there is a flaw in this I cannot see, or maybe there is a more efficient way of doing it.
  1 Comment
Luan Trinh
Luan Trinh on 26 Nov 2014
Hi everyone,
I face a problem similar to this. Could you please help?
-------------------------------------------------------------
r_si; m_si (r_si and m_si are extract from the previous function)
f_r = char(r_si); f_m = char (m_si); f_tot = f_r^2/(E*t^3/12)*f_m;
inits = 'Dy(0)=0, Dy(pi/2)=0';
u = dsolve(strcat('D2y+y=',f_r^2/f_m),inits,'si');
----------------------- This is error message.
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in strcat (line 94) s(pos:pos+len-1) = str;
Error in Moment_curvature (line 25) y = dsolve(strcat('D2y + y ==',f_tot),inits,'si');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!