How can we put element of vector to symsum? - matlab

I am trying to use symsum using Matlab. But I get a strange error.
I am trying to put elements of a vector into symsum:
a=[1,2,3,40,51,61];
syms u n
S1(u) = symsum((a(n+1)*(-u)^n)/factorial(n),n,[0,6])
Error is:
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.

First, I can see a mistake where you write a(n+1), for n going from 0 to 6. When n equals 6, you will have a(7) which doesn't exist. a only has 6 elements.
Then, you have another problem because you are indexing a with a symbol, and symbolic indexing is not allowed (see this post).
Does n really need to be a symbol and do you really need to use symsum? If not, you can try:
a = [1,2,3,40,51,61];
n = 0:5;
syms u
S1(u) = sum( sym( (a(n+1).*(-u).^n)./factorial(n) ) )
This returns:
S1(u) =
- (61*u^5)/120 + (17*u^4)/8 - (20*u^3)/3 + (3*u^2)/2 - 2*u + 1

Related

using 'solve' on vector valued function

I have a vector-valued function defined like this:
eqns =#(z) [0.2178*z(7) + 1.96*sin(z(1)) == -0.2*z(9)*cos(z(1)),...
0.7*z(9) + 1.5*z(4) + 0.2*z(7)*cos(z(1)) - 0.2*z(2).^2*sin(z(1)) == z(5)]
This is a set of two equations where I want z(7) and z(9) to become the subject of the formulae. That is to say, I want these two equations in the form z(7) = f1(z(1),z(2),z(3),z(4),z(5)) and z(9) = f2(z(1),z(2),z(3),z(4),z(5)).
I tried to do this:
[f1,f2]= solve(eqns,z(7),z(9))
but it gives me the error "Undefined function 'z' for input arguments of type
'double'."
Any help will be much appreiated
Without a declaration of z as a symbolic variable, Matlab doesn't know what z is when it tries to evaluate z(7) and z(9) in the call to solve. Therefore, z must be defined prior to the call and also the definition of eqns. Further, since you are treating z as a vector, it is easiest to declare it using sym rather than syms:
z = sym('z',[9,1]);
Lastly, as mentioned in the comments, the first input to solve is to be "specified as a symbolic expression or symbolic equation", so do not declare eqns to be an anonymous, vector-valued function as you would do, for example, fsolve; simply define it using Symbolic Variables, and it will be a Symbolic Expression by construction.
All that said, this code produces an answer for me:
z = sym('z',[9,1]);
z([1,2,4,5]) = sym(rand(4,1));
eqns = [0.2178*z(7) + 1.96*sin(z(1)) == -0.2*z(9)*cos(z(1)),...
0.7*z(9) + 1.5*z(4) + 0.2*z(7)*cos(z(1)) - 0.2*z(2).^2*sin(z(1)) == z(5)];
[f1,f2]= solve(eqns,z(7),z(9));

Solve equation with exponential term

I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)
I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.
n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f . exp(- d * pi * r^2) ' , 'r')
The answer I get is:
Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))
> In solve at 190
ans =
(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
-(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
You have several issues with your code.
1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:
n = sym(500);
f = factorial(n);
which returns an integer approximately equal to 1.22e1134 for f.
2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.
Additionally, as is stated in the R2015a documentation (and possibly earlier):
String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.
If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:
syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)
which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As #BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.
You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')

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

Evaluate Matlab symbolic function

I have a problem with symbolic functions. I am creating function of my own whose first argument is a string. Then I am converting that string to symbolic function:
f = syms(func)
Lets say my string is sin(x). So now I want to calculate it using subs.
a = subs(f, 1)
The result is sin(1) instead of number.
For 0 it works and calculates correctly. What should I do to get the actual result, not only sin(1) or sin(2), etc.?
You can use also use eval() to evaluate the function that you get by subs() function
f=sin(x);
a=eval(subs(f,1));
disp(a);
a =
0.8415
syms x
f = sin(x) ;
then if you want to assign a value to x , e.g. pi/2 you can do the following:
subs(f,x,pi/2)
ans =
1
You can evaluate functions efficiently by using matlabFunction.
syms s t
x =[ 2 - 5*t - 2*s, 9*s + 12*t - 5, 7*s + 2*t - 1];
x=matlabFunction(x);
then you can type x in the command window and make sure that the following appears:
x
x =
#(s,t)[s.*-2.0-t.*5.0+2.0,s.*9.0+t.*1.2e1-5.0,s.*7.0+t.*2.0-1.0]
you can see that your function is now defined by s and t. You can call this function by writing x(1,2) where s=1 and t=1. It should generate a value for you.
Here are some things to consider: I don't know which is more accurate between this method and subs. The precision of different methods can vary. I don't know which would run faster if you were trying to generate enormous matrices. If you are not doing serious research or coding for speed then these things probably do not matter.

MATLAB: sym function does not works

In matlab I want to create symbolic vector:
X = sym(['x_n' 'x_(n-1)' 'x(n-2)'])
however, I get
??? Error using ==> sym.sym>expression2ref at 2408
Error: Unexpected 'identifier' [line 1, col 11]
Error in ==> sym.sym>char2ref at 2378
s = expression2ref(x);
Error in ==> sym.sym>tomupad at 2147
S = char2ref(x);
Error in ==> sym.sym>sym.sym at 102
S.s = tomupad(x,'');
if I try create, e.g. just X = sym(['x_n' 'x_(n-1)']), it's ok, so what's wrong?
If you want to create a symbolic vector (or matrix) you just need one set of quotes around the square brackets:
X = sym('[x_(n) x_(n-1) x_(n-2)]')
Or
X = sym('[x_(n);x_(n-1);x_(n-2)]')
I'm assuming that you had typos in the first and third element of the vector and desire to define x_ as a function of n (if not, see below). However, this often isn't the most flexible way of building symbolic arrays. I'd use the syms function instead of sym and avoid building strings (though sometimes creating large vector/matrix equations systematically with strings can be easier):
syms x_(n)
X = [x_(n) x_(n-1) x_(n-2)]
Lastly, as the help for sym indicates, one can also automatically create vectors and matrices of enumerated variables, e.g.:
X = sym('x_',[1 3])
If you don't actually want to specify a vector of functions, then this option is very convenient.