Simplify symbolic expression in matlab and get only the coeeficents - matlab

I'm looking for finding the coefficients from the Taylor's series in Matlab. The way that I'm doing is:
% Declare symbolic expression and function:
syms x;
f = exp(x);
% Calculate the taylor expansions in a concrete point:
T = taylor(f, x, 0.5);
% And finally I simplify the expression:
coefs = simplify(T)
But the returned expression is:
Yes, the expression it's simplified, but actually what I want is:
Where each term is multiplied by his coefficient. How can I this? Options suchs as simplify(f, x, 0.5, 10, where 10 refers to the simplification step,doesn't work in my case. Also I've been seeing this question and the problem is the same:
How get to simplify a symbolic and numeric mixed expression in Matlab

Depending on how many digits you want and in what exact format you want the result, here is an example:
>> c = double(coeffs(T))
c =
Columns 1 through 4
0.999966624859531 1.000395979357109 0.498051217190664 0.171741799031263
Columns 5 through 6
0.034348359806253 0.013739343922501
>> digits 15
>> x.^(0:numel(c)-1) * sym(c,'d').'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531
EDIT
Note that you could also use vpa( ) instead of converting to double first:
>> c = coeffs(T)
c =
[ (2329*exp(1/2))/3840, (233*exp(1/2))/384, (29*exp(1/2))/96, (5*exp(1/2))/48, exp(1/2)/48, exp(1/2)/120]
>> x.^(0:numel(c)-1) * vpa(c).'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

Related

How to do summation by using for loop in matlab [duplicate]

I have the following series
I tried this code but it does not print the final result...instead gives a long line of numbers!
syms n
y = symsum(1/sqrt(n),[1,100])
Result:
y =
2^(1/2)/2 + 3^(1/2)/3 + 5^(1/2)/5 + 6^(1/2)/6 + % and so on....
So the question is how to produce a final number as answer?!
Should I go with a script like this instead?
y = 0;
for i = 1:1:100
y = y + (1/sqrt(i));
end
disp(y);
To answer the original question, you can convert the symbolic expression you initially got using double, to convert from a symbolic to a numeric value:
y = double(y)
Or actually:
syms n
y = double(symsum(1/sqrt(n),[1,100]))
and you get 18.5896.
Additionally, you can use eval to evaluate the symbolic expression (thanks Luis Mendo).
Yay!
how about dropping the loop and use this instead:
n=1:100
result = sum(1./sqrt(n))
>> result =
18.5896
I'm not sure if you want to use the symbolic sum of series function in your case since you are only dealing with a simple function.

Sum of series in matlab using symsum

I have the following series
I tried this code but it does not print the final result...instead gives a long line of numbers!
syms n
y = symsum(1/sqrt(n),[1,100])
Result:
y =
2^(1/2)/2 + 3^(1/2)/3 + 5^(1/2)/5 + 6^(1/2)/6 + % and so on....
So the question is how to produce a final number as answer?!
Should I go with a script like this instead?
y = 0;
for i = 1:1:100
y = y + (1/sqrt(i));
end
disp(y);
To answer the original question, you can convert the symbolic expression you initially got using double, to convert from a symbolic to a numeric value:
y = double(y)
Or actually:
syms n
y = double(symsum(1/sqrt(n),[1,100]))
and you get 18.5896.
Additionally, you can use eval to evaluate the symbolic expression (thanks Luis Mendo).
Yay!
how about dropping the loop and use this instead:
n=1:100
result = sum(1./sqrt(n))
>> result =
18.5896
I'm not sure if you want to use the symbolic sum of series function in your case since you are only dealing with a simple function.

Use of (ilaplace) give different result through sym variable and directly

I have a symbolic variable, which contain, for example:
p =
(9311.0*s + 6.12e9)/(s^2 + 8500.0*s + 3.61e11)
where s - also symbolic.
Then, if I use inverse laplace through variable p, then result is
>>result=vpa(ilaplace(p,s,n),3)
result =
exp(n*(- 4255.0 + 6.01e5*i))*(4666.0 - 5066.0*i) + exp(n*(- 4255.0 - 6.01e5*i))*(4666.0 + 5066.0*i)
But if I put expression directly, I will get what I expect (by formula in Korn's book or by definition):
vpa(ilaplace((9311.0*s + 6.12e9)/(s^2 + 8500.0*s + 3.61e11),s,n),3)
ans =
9311.0*exp(-4255.0*n)*(cos(6.01e5*n) + 1.09*sin(6.01e5*n))
Why? And what should I do to get right answer through variable?
P.S. vpa - is not influenced on main goal. It only left 3 digits in this case after point.
P.S.S. Added more code:
t = tf(linsys1) %linsys1 - from simulink circuit
%get coefficients from transfer function t
[num,den] = tfdata(t);
syms s n real
% convert transfer function to symbolic
t_sym = poly2sym(cell2mat(num),s)/poly2sym(cell2mat(den),s);
functionInMuPad=['partfrac(',char(t_sym),',s,Domain = R_)']; %collect expression in string format
simpleFraction=evalin(symengine,functionInMuPad); % sum of simple fractions (only MuPad allows get denominator of 2nd order)
functionInMuPad2=['op(',char(simpleFraction),')']; %collect expression in string format
vectorOfOperand=evalin(symengine,functionInMuPad2); % vector of simple fractions
for k=1:length(vectorOfOperand)-1
z(k,1)=ilaplace(vectorOfOperand(k),s,n);
end
So, something wrong with vectorOfOperand.
ilaplace(vectorOfOperand(1)) gives complex result, but if copy (ctrl+c) value of vectorOfOperand(1) and make newVariable=Ctrl+V, then ilaplace(newVariable) - it's ok either in command window or in m-file:
bbb =(9313.8202564498255348020392071286*s + 6122529964.4040716985063406588769)/(8500.4056471180697831891961467533*s + s^2 + 360665607284.96754103451618653904);
ilaplace(bbb,s,n)
ans=9311.0*exp(-4255.0*n)*(cos(6.01e5*n) + 1.09*sin(6.01e5*n)) %after vpa
Kind of magic anyway. vectorOfOperand - is sym. I even made this:
vectorOfOperand=char(vectorOfOperand);
vectorOfOperand=sym(vectorOfOperand); it doesn't help..

symbolic matlab matrix to latex

Consider the symbolic matlab expression
e = (a_1_1 + a_2_2)*(b_1_1 + b_2_2)
Using latex(e) this yields
\left({{a_{1}}}_{1} + {{a_{2}}}_{2}\right)\, \left({{b_{1}}}_{1} + {{b_{2}}}_{2}\right)
Is it possible to [somehow] use comma as separator between the indices, i.e. to get
\left(a_{1,1} + a_{2,2} \right)\,\left(b_{1,1} + b_{2,2}\right)
I'd be interested in an easy way to do it. An ugly way would be:
eqn = latex(e);
eqn1 = regexprep(eqn,'{{','');
eqn2 = regexprep(eqn1,'}}}_{',',');

how do I find the value of a parameter when others are given and integral is zero using matlab?

I have a function
function y = testf(x,F,phi,M,beta,alpha)
y = -((F+(1 + phi.*cos(2.*pi.*x))).*M.^3.*(cosh((1 + phi.*cos(2.*pi.*x)).*M)+M.*beta.*sinh((1 + phi.*cos(2.*pi.*x)).*M)))./((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M))- (alpha.*(M.^2.*(F+(1 + phi.*cos(2.*pi.*x))).*(-1+2.*(1 + phi.*cos(2.*pi.*x)).^2.*M.^2+ cosh(2.*(1 + phi.*cos(2.*pi.*x)).*M)-2.*(1 + phi.*cos(2.*pi.*x)).*M.*sinh(2.*(1 + phi.*cos(2.*pi.*x)).*M)))./(8.*((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M)).^2));
integrating with
q = quad(#(x) testf (x, F, phi,M, beta, alpha), 0, h);
when q = 0 and x,F,phi,M,beta, how do I find alpha and draw the streamline?
It would be great if you gave some numbers, but here is how you would start this. It assumes you are using a version of Matlab that has MuPad as the Symbolic Engine.
First of all, I wouldn't use quad because symbolic expressions will be involved, use int instead.
If I understood you correctly, have the values of x, F, phi, M, beta and you would like to solve for alpha when q = 0
%define the known variables first
syms alpha %defining symbolic object
Now, the following may not work because it's a huge function:
q = int(y,x,0,h)
If it did, all you have to do is solve and evaluate the results for alpha (this may not work as well):
alpha = eval( solve( 'q' , alpha ) )
If the above didn't achieve anything, you might what to look at the 'IgnoreAnalyticConstraints' option.