Filter löschen
Filter löschen

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);

5 Ansichten (letzte 30 Tage)
y = 100:400; h2a = 310 + 7.85*y + 0.00194*y.^2; cf2a = 1.5*h2a; df2a=diff(cf2a,y); eq2 = df2a - L==0;

Antworten (2)

Adithya
Adithya am 3 Mär. 2023
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.

Walter Roberson
Walter Roberson am 3 Mär. 2023
Bearbeitet: Walter Roberson am 3 Mär. 2023
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.

Community Treasure Hunt

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

Start Hunting!

Translated by