after running this i am getting the error - Error using diff Difference order N must be a positive integer scalar. Error in unit1 (line 14) df2a=diff(cf2a,y);
15 次查看(过去 30 天)
显示 更早的评论
y = 100:400; h2a = 310 + 7.85*y + 0.00194*y.^2; cf2a = 1.5*h2a; df2a=diff(cf2a,y); eq2 = df2a - L==0;
0 个评论
回答(2 个)
Adithya
2023-3-3
The error message you are seeing indicates that the second input argument to the "diff" function should be a positive integer that specifies the order of the difference. In your code, you have passed the variable "y" as the second argument, which is causing the error.
The "diff" function calculates the differences between adjacent elements in an array, but it requires a scalar integer as the second input argument to specify the order of the difference. If you want to calculate the first-order difference of the "cf2a" array with respect to the "y" array, you can simply use the "diff" function without specifying the second argument, like this:
df2a = diff(cf2a);
This will calculate the first-order difference of the "cf2a" array, which should have one less element than the "cf2a" array.
However, if you want to calculate the derivative of the "cf2a" array with respect to the "y" array, you can use the "gradient" function instead of the "diff" function. Here's how you can modify your code to calculate the derivative of "cf2a":
L = 1; % assume the value of L to be 1
y = 100:400;
h2a = 310 + 7.85*y + 0.00194*y.^2;
cf2a = 1.5*h2a;
df2a = gradient(cf2a, y);
eq2 = df2a - L == 0;
This should calculate the derivative of "cf2a" with respect to "y" and store it in the "df2a" variable.
0 个评论
Walter Roberson
2023-3-3
编辑:Walter Roberson
2023-3-3
There are two functions named diff(). The one that operates on numbers such as would be in cf2a is the numeric differences function.
There is also the calculus differentiation, but that only applies to sym and symfun from the symbolic toolbox.
Remember that in most cases when you have an expression such as
cf2a = 1.5*h2a
that MATLAB does the calculation and then promptly discards all memory of how the result was created, so for numeric values you cannot take the derivative of cf2a and expect it to go back to the variables involved in the creation of the numeric values and expect it to take a derivative.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!