How to plug in values for partial derivatives results

22 views (last 30 days)
I have a function to solve f for partial derivatives at x1, x2, x3, and x4. I want to assign a value to x2, for example (x2=5) but I want it to apply after the partial derivative has been solved by MATLAB.
Here is my code:
syms x1 x2 x3 x4;
f=2*x1*x2+x3*x4;
diff(f,x1);
diff(f,x2);
diff(f,x3);
diff(f,x4);
and the results yield:
ans =2*x2
ans =2*x1
ans =x4
ans = x3
BUT now I want to take these results and find a numeric value when x1, x2, x3, and x4 are assigned to certain values. (Example: if x2=5, then 2*5=10)
  1 Comment
Gemma Arterton
Gemma Arterton on 15 Apr 2021
Let f(x,y)=y3x2f(x,y)=y3x2. Calculate fx(x,y)fx(x,y).
Solution: To calculate fx(x,y)fx(x,y), we simply view yy as being a fixed number and calculate the ordinary derivative with respect to xx. The first time you do this, it might be easiest to set y=by=b, where bb is a constant, to remind you that you should treat yy as though it were number rather than a variable. Then, the partial derivative fx(x,y)fx(x,y) is the same as the ordinary derivative of the function g(x)=b3x2g(x)=b3x2. Using the rules for ordinary differentiation, we know that
dgdx(x)=2b3x.dgdx(x)=2b3x.Now, we remember that b=yb=y and substitute yy back in to conclude that
fx(x,y)=2y3x.fx(x,y)=2y3x.
Example 2
For the same ff, calculate fy(x,y)fy(x,y).
Solution: This time, we'll just calculate the derivative with respect to yy directly without replacing xx with a constant. We just have to remember to treat xx like a constant and use the rules for ordinary differentiation. We don't touch the x2x2 and only differentiate the y3y3 factor to calculate that
fy(x,y)=3x2y2.
try to use these two methods i also have same issue but my issue is resolve this this code

Sign in to comment.

Answers (1)

Yukthi S
Yukthi S on 16 Apr 2024
Hi Alaura
I've noted that you essentially want to substitute numerical values after the partial derivative has been calculated. You can use “subs” function to do the same. Here is the MathWorks documentation link: https://www.mathworks.com/help/releases/R2019b/symbolic/subs.html?searchHighlight=subs&s_tid=doc_srchtitle#:~:text=subs(s)-,Description,-example
You can modify the code as below to get the desired result:
syms x1 x2 x3 x4;
f = 2*x1*x2 + x3*x4;
% Compute the partial derivatives
pa_df_x1 = diff(f, x1); %pa_df_x1 is the result of partial differentiation of f wrt x1
pa_df_x2 = diff(f, x2);
pa_df_x3 = diff(f, x3);
pa_df_x4 = diff(f, x4);
% Assign value to x2
x2_val = 5; % Example value for x2, as you specified
% Substitute the values into the derivatives
df_dx1_val = subs(pa_df_x1, x2, x2_val);
% Display the results
sprintf('The value of df/dx1 after substitution is: %d',df_dx1_val)
ans = 'The value of df/dx1 after substitution is: 10'
Hope this helps!

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!