I am trying to find second derivative of a function, but while initializing my symbols i am getting the following error:
Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'sym'.
The commands I am using are:
syms x a b c L;
u = (a*x(x-L))+(b*x((x^2)-(L^2)))+(c*x((x^3)-(L^3)));
"u" is my function.
I don't know much about MATLAB's symbolic capabilities, but that error is coming from the pieces like
x(x-L)
which MATLAB is interpreting as an indexing operation. Did you mean multiplication there? I.e.
x*(x-L)
Related
I have a density function f_N which is defined as follows (K_nu(z) is the modified Bessel function):
I want to compute the following integral for each value of N:
The following is the implementation of the above in matlab.
for N=1:100
syms z
f =#(z) (1/(gamma(N)*sqrt(pi))*(z/2).^(N-0.5).*besselk(0.5-N,z));
g = #(z) f(z).*log(f(z));
val=integral(g,0,Inf);
But when I run the above code, it's always returning NaN for varoious values of N with the following warning:
Warning: Infinite or Not-a-Number value encountered
Can someone suggest a simple way to do this or avoid this issue?
I don't think you are doing what you think you are doing. Your declaration of z as a symbol will get overridden by the function handle definition. That means the integral is not a symbolic one, but a numeric one. So what you simply need to do is to drop the function handle notation "#(z)" and do the integral symbolically...
My guess, without thoroughly analysing this, is that one of the points in you integration domain [0,inf] yields a value f (x)=inf, which would destroy a numeric integration technique, but perhaps not a symbolic one.
I am running a MATLAB code which solves a set of non-linear simultaneous equations. The code can be found here. The data input for the code (in excel) can be found here.
I encounter the following error:
Error using sym/subsasgn (line 733)
Indexed assignment to empty SYM objects is supported only in the 0-by-0 case.
Error in Solution (line 69)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
Does anyone have any idea how I can resolve this?
When declaring a symbolic variable, you have to specify the dimensions, otherwise it's a scalar. Instead of syms x; use sym and set the dimension argument:
x=sym('x',[3,4])
Replace [3,4] with the dimensions you want.
Using the code,
syms x(t)
y=x^2
diff(y,t)
diff(y,x)
I get the following error:
2*D(x)(t)*x(t)
Error using sym/diff (line 26)
All arguments, except for the first one, must not be symbolic functions.
Is there a way to tackle this? Thanks for your time.
I dont know much about the Symbolic Math Toolbox, but taking a derivative wrt to a function does not seem to be supported (at least in a direct fashion) for diff.
You can substitute a variable, compute a derivative, and substitute the function back. Like so:
syms z
subs(diff(subs(y,x,z),z),z,x)
ans(t) = 2*x(t)
I am trying to fit function F to experimental data.
x_tem and yd are both vectors of size (12,1). The function should find the best
fitting value y_tau of the function to the experimental data.
I just can't find the mistake - matlab is showing me the error :
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
The code is:
x_tem=Temp_aero_korrektur(:,1);
yd=Temp_aero_korrektur(:,2);
F = #(y_tau,x_tem)((-1)*((273.15-x_tem)*(273.15-y_tau(1))*8.314* (((17.62*x_tem)/(243.12+x_tem))-((17.62*y_tau(1))/(243.12+y_tau(1)))))/(40714.53));
yd_tau = lsqcurvefit(F,-40,x_tem,yd);
There are two possibilities here. One is that you do actually want to use matrix operations in your objective function (so that, for example, x_tem/x_tem gives a single scalar value using mrdivide). If this is the case then you should be calling lsqcurvefit with the transpose of x_tem
yd_tau = lsqcurvefit(F,-40,x_tem',yd);
The other option is that you actually meant to calculate your objective function on each value of x_tem (so, for example, using x_tem./x_tem to give a vector the same length as x_tem). If this is the case then your objective function should be
F = #(y_tau,x_tem)((-1)*((273.15-x_tem).*(273.15-y_tau(1)).*8.314.* (((17.62*x_tem)./(243.12+x_tem))-((17.62*y_tau(1))/(243.12+y_tau(1)))))/(40714.53))
(See documentation for times and rdivide for element-wise operations)
I'm trying to derive Lagrangian equations of motion in Matlab using the symbolic toolbox. This involves partial derivatives of a function and your coordinates, but matlab seems to not accept this.
So I would do this in Matlab:
syms t x(t) % t: time, x(t) position dependent on time
m = sym('m'); % mass, a constant parameter
T = m/2*diff(x,t)^2; % kinetic energy
dTdx = diff(T,x);
ddTdxDotdt = diff( diff(T,diff(x,t)), t);
But as soon as I try to differentiate anything in x (or diff(x,t)), Matlab complains:
Error using mupadmex
Error in MuPAD command: The variable is invalid. [stdlib::diff]
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
Does anyone know the proper way of handling this?
Matlab ought to be able to do this as you have it written, but I think that it doesn't like taking derivatives with respect to a symfun. Type whos in the command window and you'll see that x is listed as a symfun while t is just a sym. The help for diff kind of indicates this limitation. It won't event try to take the derivative of a constant with respect to x(t): diff(1,x) "complains" just the same. Unless newer versions of Matlab fix this (I'm on R2012b) I think you only option may be to come up with a scheme using two instances of x.