Assigning a symbolic expression with sprintf command

3 views (last 30 days)
Hello guys, I'm trying to implement a code where I define a symbolic function and I want to find out its partial derivatives (first and second) also in a symbolic way. The example is below:
prompt = ('\n Type the function: ');
f = sym(input(prompt,'s'));
prompt = ('\n Type the number of independent variables: ');
n1 = (input(prompt));
n2=n1;
for i=1:1:n1
syms(sprintf('x%d', i));
syms(sprintf('df%d', i));
for j=1:1:n2
syms(sprintf('df%d%d', i,j));
end
end
For n=2, I get the following result (all symbolic variables): x1, x2, df1, df2, df11, df12, df21 and df22.
Now I want to assign to each of these variables the proper partial derivative, for example:
df1 = diff(f,x1)
df2 = diff(f,x2)
df11 = diff(df1,x1)
df12 = diff(df1,x2) and so on...
How can I put these diff commands in a loop??
  1 Comment
Karan Gill
Karan Gill on 14 May 2017
To confirm, another user is interactively entering the function? That is, you are not defining the function yourself, correct?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!