How to perform implicit derivative? - matlab

I'm going to perform some symbolic calculus for a project and I'm starting with something simple.
I'm ttrying to calculate the derivative of L function in respect to variable fi, but using the following code I get the error shown.
syms x(t) y(t) fi(t) m IR t;
L = 1/2*(m*(diff(x(t),t)^2+diff(y(t),t)^2) + IR*diff(fi(t),t)^2)
D1 = diff(L,diff(fi(t),t))
ERROR:
Error using sym/diff (line 70)
Second argument must be a variable or a nonnegative integer specifying the number of differentiations.
Could anyone tell me what's going on? Thanks.
[1]: https://i.stack.imgur.com/fTpEM.png

You have a nested diff in the second line. For the outer diff, the second argument cannot be the inner diff - it should be a variable or non-negative integer.

Related

symsum function error - invalid indexing or function definition?

I'm attempting to sum the following using the symsum function in MATLAB:
sum (from q=0 to 5) [a(q+1)*x(2)^q]
where a=[a0, a1, ..., a5], x=[x(1), x(2), ...] where x(1), x(2), ... are scalars.
The sum is a0 + a1x(2)+a2x(2)^2 +...+a5x(2)^5.
I've used the following code:
syms q a x
f=a(q+1)*x(2)^q
symsum(f, q, 0, 5)
where x(2)= -4.9.
However, the above code returns "Invalid indexing or function definition".
Using f=x(2)^q does not result in the error, however, using f=a(q+1) does return the error. Therefore the problem lies within the a(q+1) term.
Any help is greatly appreciated!
It doesn't matter a is matrix of symbolics or non-symbolics.
Every call to a matrix need an index(a number, real number like 1,2,3,4,...)
When q is a symbolic it means q really doesn't equal to any number, so a(q)
has no meaning because q has no equality to any number. So programs make error and stops before going more.
>>a=0:5;
>> a(1)
ans =
0
>>a(q) % ?-->do you know the real value of `q` right now? NO,nobody knows!
MAKES ERROR

What does this MATLAB expression mean?

I have the following MATLAB function definition:
function dv = rc(t,v)
dv(1) = -0.1*v(1);
I'm trying to understand what it's doing. I can understand the expression and definition of the function, but why is there a (1) after dv and v? What does that do?
v(1) takes the first element of v if v is not a scalar (note that Matlab indices starts from 1 and not from 0).
The output of -0.1*v(1) is assigned to dv, specifically to the first element of dv, that is dv(1).
Note that since dv is not defined and is created by the assignment expression it is quite pointless to add the subscript (1) for this assignment.

Solving an inequality with Matlab

I have to solve an inequality but it is too hard to do it by hand. Therefore, I would like to use Matlab. Let a = [(k-3)*sqrt(v)]/s and b = 1.08148*a^2-epsilon, where epsilon = 10^(-6). The inequality that needs to be solved is:
q(a,b) < s*sqrt(v)
where s and v are known. More precisely I want to solve the above inequality for k (which occurs in a and b). Now, the problem is that q(a,b) is the greatest real root of the quartic polynomial:
(48*a^2+16*b)*x^4 - (40*a^3+168*a*b)*x^3+(-45*a^4+225*a^2*b+72*b)*x^3+(27*a^2*b-162*a*b^2)*x+27*b^3
I tried to run this:
syms x z
av = ((x-3)*sqrt(v))/s;
Q = max(roots([48*z^2+16*(1.08148*z-eps), -40*z^3-168*z*(1.08148*z-eps), -45*z^4+225*z^2*(1.08148*z-eps)+72*(1.08148*z-eps)^2, 27*z^3*(1.08148*z-eps)-162*z*(1.08148*z-eps)^2, 27*(1.08148*z-eps)^3]));
F = compose(Q,av);
solve(F-skew*sqrt(var)<0, x)
but Matlab keeps giving the following error:
Error using sym/max (line 97)
Input arguments must be convertible
to floating-point numbers.
Error in Testt (line 13)
R = max(roots([2048,
-6912*((x-3)sqrt(var)/skew)^2,
8088((x-3)sqrt(var)/skew)^4,
-3600((x-3)sqrt(var)/skew)^6,
375((x-3)*sqrt(var)/skew)^8]));
Perhaps someone has a better idea to solve it? The best way would be if I had an explicit expression for the greatest real root q of the quartic in function of a and b. However, this explicit expression is too lengthy to use.
The error information seemed to say the max() function do not support sym x z.
Because the returned result of roots() includes x and z.But Matlab doesn't know the value of x and z,so it can't calculate the value of the roots and then can't compare them.
Maybe you should improve your algorithm.

Error using integral: A and B must be floating-point scalars

I want to evaluate the simple example of integral
a = max(solve(x^3 - 2*x^2 + x ==0 , x));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
and the error is
Error using integral (line 85)
A and B must be floating-point scalars.
Any tips? The lower limit of my integral must be a function, not a number.
The Matlab command solve returns symbolic result. integral accepts only numeric input. Use double to convert symbolic to numeric. As your code is written now, already max should throw an error due to symbolic input. The following works.
syms x;
a = max(double(solve(x^3 - 2*x^2 + x)));
fun = #(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)
Output: 1.9331.
the lower limit of my integral must be a function, not a number
integral is a numeric integration routine; the limits of integration must be numeric.
Check values of a by mouse over in breakpoint or removing the ; from the end of the line so it prints a. Based on the error, a is not a scalar float. You might need another max() or double() statement to transform the vector to a single value.
Solve Help : http://www.mathworks.com/help/symbolic/solve.html
Integral Help : http://www.mathworks.com/help/ref/integral.html

why isn't the result a scalar?

i'm stuck with this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
yres(1)=((u - uc).^2) + ((y - yc).^2) -(d.^2);
i don't understand, why this won't get a skalar?since the elements are all scalar. what should be changed to get a scalar?
best regards
edit: thanks sloede, all inputs are scalar, but i still get this error
In an assignment A(I) = B, the number of elements in B and I must be the
same.
Error in myfun (line 7)
yres(1)=sqrt(((u - uc).^2) + ((y - yc).^2) ) -d;
Error in fsolve (line 241)
fuser = feval(funfcn{3},x,varargin{:});
Error in modfsolve (line 26)
x= fsolve(#myfun,x0,options,uc,d,spacing_amplitude,spacing_width);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue.*
The "." before an operator means that the following operation should be applied element-wise and not on the vector as a whole. Thus
a = b.^2
will give you as a result all elements of b squared and saved back to a. Therefore, in your code statement above, if any of u, uc, y, yc, d are not scalar but a vector, your result will be a vector as well.
Otherwise there seems to be nothing wrong with your code.
read the documentation of fsolve: http://www.mathworks.nl/help/toolbox/optim/ug/fsolve.html
it states:
fun
The nonlinear system of equations to solve. fun is a function that accepts a vector x and returns a vector F, the nonlinear equations evaluated at x.
Obviously your function myfun doesn't handle vector input.
You can solve this by adding the following construction inside your function (and of course change it to your needs/your parameters):
function out = myfun(in)
if ~isscalar(in)
% assuming it's a matrix or vector
out = reshape(arrayfun(#myfun,in(:)),size(in));
else
% your actual function execution statements
out = dostuffon(in);
end
end
or properly vectorize your function (if that's possible)