I want to evaluate a definite integral with variable precision arithmetic in Matlab. It can be done using symbolic math toolbox in this way:
syms x
f = (x.^2000).*((1-x).^4000)
vpa(int(f,0,1))
This gives me the answer of the integral with variable precision arithmetic.
But I like to evaluate the integral without symbolic math toolbox. I can use the command 'integral' to calculate the integral but since the integral is calculated in fixed precision, it returns zero, i.e. the output of the following code is zero.
f = #(x) (x.^2000).*((1-x).^4000)
integral(f,0,1)
How can I combine vpa with numerical integration without using symbolic math toolbox?
Impossible, because the vpa function is a part of the symbolic math toolbox. If you are using vpa, you are using this toolbox.
Related
I apologize if I'm asking something trivial. I know that MATLAB's symsum (of the Symbolic Math Toolbox) can be used for series. For instance,
syms k
symsum(2^k/factorial(k),k,1,Inf) % output exp(2) - 1
The series whose general term is
is a divergent one. When I try symsum with this series I get
symsum(log(k+1)/k,k,1,Inf)
That is, MATLAB returns the series unevaluated. However, I wanted a behavior similar to Mathematica's.
Sum[Log[r + 1]/r, {r, 1, Infinity}]
(*Output: During evaluation of In[1]:= Sum::div: Sum does not converge.*)
Is it possible to get such an output from MATLAB's Symbolic Math Toolbox?
There's a warning when I tried to do an integration with MATLAB!
syms z
int(exp(sin(z)),z)
Warning: Explicit integral could not be found.
The expression exp(sin(x)) does not have a known analytical formula for it's indefinite integral. Mathematica agrees with Matlab on this: http://m.wolframalpha.com/input/?i=Integral+e%5Esin%5Bx%5D+dx.
While you can't evaluate the indefinite integral, you can compute definite integrals numerically to an arbitrary precision.
I have an integration function which does not have indefinite integral expression.
Specifically, the function is f(y)=h(y)+integral(#(x) exp(-x-1/x),0,y) where h(y) is a simple function.
Matlab numerically computes f(y) well, but I want to compute the following function.
g(w)=w*integral(1-f(y).^(1/w),0,inf) where w is a real number in [0,1].
The problem for computing g(w) is handling f(y).^(1/w) numerically.
How can I calculate g(w) with MATLAB? Is it impossible?
Expressions containing e^(-1/x) are generally difficult to compute near x = 0. Actually, I am surprised that Matlab computes f(y) well in the first place. I'd suggest trying to compute g(w)=w*integral(1-f(y).^(1/w),epsilon,inf) for epsilon greater than zero, then gradually decreasing epsilon toward 0 to check if you can get numerical convergence at all. Convergence is certainly not guaranteed!
You can calculate g(w) using the functions you have, but you need to add the (ArrayValued,true) name-value pair.
The option allows you to specify a vector-valued w and allows the nested integral call to receive a vector of y values, which is how integral naturally works.
f = #(y) h(y)+integral(#(x) exp(-x-1/x),0,y,'ArrayValued',true);
g = #(w) w .* integral(1-f(y).^(1./w),0,Inf,'ArrayValued',true);
At least, that works on my R2014b installation.
Note: While h(y) may be simple, if it's integral over the positive real line does not converge, g(w) will more than likely not converge (I don't think I need to qualify that, but I'll hedge my bets).
When using MATLAB's lsqnonlin function, I am trying to give a user-defined Jacobian matrix, as described in the documentation.
The output of the objective function used in lsqnonlin should be a vector of unsquared values, which, when squared and summed, give the energy. However, should the Jacobian be the partial derivates of the squared or unsquared values?
The unsquared values is correct.
I am working on a function that will generate polynomial interpolants for a given set of ordered pairs. You currently input the indexes of the node points in one vector, and the values of the function to be interpolated in a second vector. I then generate a symbolic expression for the Lagrange polynomial that interpolates that set of points. I would like to be able to go from this symbolic form to a vector form for comparison with test functions and such. That is, I have something that generates some polynomial P(x) in terms of some symbolic variable x. I would like to then sample this polynomial to a vector, and get values for the polynomial over (for example) linspace(-1,1,1000). If this is possible, how do I do it?
I guess I'll include the code that I have so far:
function l_poly = lpoly(x,f)
% Returns the polynomial interpolant as computed by lagrange's formula
syms a
n=size(x,2);
l_poly_vec = 1;
l_poly=0;
for k=1:n,
for l=1:n,
if (k ~= l)
l_poly_vec=l_poly_vec*(a-x(l))/(x(k)-x(l));
end
end
l_poly=l_poly+f(k)*l_poly_vec;
l_poly_vec = 1;
end
I plan on adding a third (or possibly fourth) input depending on how I can solve this issue. I'm guessing I would just need the length of the vector I want to sample to and the endpoints.
If I understand you correctly, you've constructed a Lagrange interpolating polynomial using the symbolic toolbox and now wish to evaluate it over a vector of values. One way to do this is to use the function sym2poly to extract the coefficients of the symbolic polynomial, and then use polyval to evaluate it. Alternatively, you could use matlabFunction to convert your symbolic expression into a regular Matlab function; or use subs to substitute in a numeric value for 'x'.
However, you would probably be better off avoiding the symbolic toolbox altogether and directly constructing the coefficients of the Lagrange interpolating polynomial, or, better yet, use a different interpolation scheme altogether. The function interp1 might be a good place to start.