How to plug in values for partial derivatives results
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Gemma Arterton
2021년 4월 15일
Let f(x,y)=y3x2f(x,y)=y3x2. Calculate ∂f∂x(x,y)∂f∂x(x,y).
Solution: To calculate ∂f∂x(x,y)∂f∂x(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 ∂f∂x(x,y)∂f∂x(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
∂f∂x(x,y)=2y3x.∂f∂x(x,y)=2y3x.
Example 2
For the same ff, calculate ∂f∂y(x,y)∂f∂y(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
∂f∂y(x,y)=3x2y2.
try to use these two methods i also have same issue but my issue is resolve this this code
Source: Matlab experts Series
답변 (1개)
Yukthi S
2024년 4월 16일
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)
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!