what is wrong with my partial differential codes?!

3 views (last 30 days)
why when i run this attached code, MATLAB show me the error bellow:
"Error using diff
Difference order N must be a positive integer scalar.
Error in TEST1 (line 70)
Jr = [diff(px,pz) diff(px,psi) diff(px,theta) ;..."

Answers (1)

Walter Roberson
Walter Roberson on 30 Nov 2015
You call solve() and extract a value from the result. After that you never use any symbolic variables. You originally define px as symbolic, but in the line before the diff() that you are having trouble with, you assign a numeric value to px. And early on you assigned
pz = -470;
so your pz is numeric.
You are therefore trying to diff(A numeric expression, Another Numeric Expression) and that invokes the regular MATLAB diff() operator rather than symbolic differentiation.
To use symbolic differentiation, the first parameter to diff() must be a symbolic expression, preferably one that involves the symbolic variable that you name in the second parameter.
  2 Comments
armin mohsenpour
armin mohsenpour on 1 Dec 2015
actually, i didn't understand your point exactly. may you offer me how to fix it?
Walter Roberson
Walter Roberson on 1 Dec 2015
There are two different routines named diff() in MATLAB.
When you call diff() with a number X (or vector of numbers or array of numbers) as the first parameter, then you what you get is a routine that does essentially
X(2:end,:) - X(1:end-1,:)
If a second parameter N is given in this case, then the second parameter says how many times to repeat that operation, the "N'th difference".
This is the case that you are invoking, because your px is purely numeric when the diff() is called.
If you have the Symbolic Toolkit (which you do) then there is a second diff() which is only invoked if the first parameter is a symbolic expression. For example,
syms x %x is symbolic
diff(cos(x), x) %symbolic differentiation, returns -sin(x)
If you were to do
x = pi/7; %x is no longer symbolic, now it is numeric
diff(cos(x),x)
then because x is no longer symbolic, cos(x) would be calculated as a particular double precision number 0.900968867902419 and you would be calling
diff(0.900968867902419,0.448798950512828)
This is the regular diff(), not the symbolic diff, and it would fail because 0.448798950512828 is not an integer that can be used to say how many times to take the numeric difference of adjacent elements.
diff(symbolic_expression) is differentiation. This is what you think you are invoking
diff(numeric_expression) is numeric difference. This is what you are invoking.
The only fix I can suggest is that you go back and re-examine your formula. Why are you attempting to take a symbolic differentiation with regards to pz when none of your calculations to that point use pz?
You probably need to use
syms pz
and define your calculation of px to use pz at some point. Then you would be able to diff(px,pz) . If the pz does not vanish in the resulting formula, it might then make sense to
subs(J, pz, -470)
to substitute in the particular pz value of -470

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!