I am trying to write a function which takes the value x and y in cartesian coordinates as the input and outputs the polar and trigonometric forms. I would like the output to contain the exponential and sin/cos rather than the actual value. For example, if the cartesian is z=1i, I would like the function to output z=sqrt(2)e^(ipi/4) and z=sqrt(2)(cos(pi/4)+Isin(pi/4). How can I do this?
function coordinates(x,y)
r=sqrt(x.^2+y.^2);
theta=atan(y./x);
polarcoord=rexp(itheta)
trigcoord=r*(cos(theta)+i*sin(theta))
end
This gives me the following output :
polarcoord =
1.0000 + 1.0000i
trigcoord =
1.0000 + 1.0000i
thanks
Welcome to SO! You are using functions (sqrt(x) calculates the actual square root of x, exp(x) the exponential, ... So that is why you get the actual value, not the formula expression.
So, a possible way to output the formula is using strings to print the functions without calling them. Then, what you need to do is calculate the values you want inside the sqrt() and the fraction which * pi and add them to a string array. We can do that using square brackets [ ], the function num2string() and the operator + to add strings in the same string array:
function coordinates(x,y)
[num,dem] = rat(atan(y./x)/pi);
% rat converts theta to fraction
% divide by pi to extract pi from theta
root = ["sqrt(" + num2str(x^2+y^2) + ")"]; % i.e "sqrt(2)"
pi_value = ["(" + num2str(num) + "*pi/" + num2str(dem) + ")"];
% i.e "(1*pi/4)" o "(2*pi/5)"...
polarcoord = ["z = " + root + "e^(i" + pi_value]
trigcoord = ["z = " + root + "(cos" + pi_value + ")+i*sin" + pi_value]
end
Example:
coordinates(1,1)
Outputs:
polarcoord = "z = sqrt(2)e^(i(1*pi/4)"
trigcoord = "z = sqrt(2)(cos(1pi/4))+isin(1*pi/4)"
To calculate the fraction we use rat() function to obtain numerator and denominator from the double output of atan(y./x) and we divide by pi to extract its value from the theta value (we are representing pi already in the string).
Related
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
I have 3 data matrices A,B,C (all 3x3), with which I use the following code to calculate roots of D(p)X = 0
syms p
D = A + B*p + C*(p^2)
solP = double(solve(det(D)))
From this, I get 6 values for solP. But when I try substituting it back into the symbolic matrix D, as follows, I get non-zero values of det(D) sometimes
for i = 1:6
p = solP(i)
det(double(subs(D)) % Should be zero always as we are substituting roots
end
Please help me understand this behaviour.
EDIT ::
Example :
A =
1.0e+11 *
4.8976 7.0936 6.7970
4.4559 7.5469 6.5510
6.4631 2.7603 1.6261
B =
1.0e+11 *
3.9223 7.0605 0.4617
6.5548 0.3183 0.9713
1.7119 2.7692 8.2346
C =
1.0e+11 *
6.9483 0.3445 7.6552
3.1710 4.3874 7.9520
9.5022 3.8156 1.8687
solP =
0.1061 + 0.0000i
1.5311 + 0.0000i
-0.3432 + 0.9356i
-0.3432 - 0.9356i
0.4228 - 0.5465i
0.4228 + 0.5465i
det(D) =
2.2143e+19
-5.4911e+20
-8.6415e+19 + 4.5024e+19i
-8.6415e+19 - 4.5024e+19i
-1.4547e+19 + 9.1135e+19i
-1.4547e+19 - 9.1135e+19i
The problem is related to the relative accuracy of floating point values, typically 1e-16.
The input matrices are of the order 1e+11 - 1e+12, the solution is of the order 1e+0, so the elements of D are also of the order 1e+11 - 1e+12. To calculate a determinant of a 3x3 matrix, one should take products of three matrix elements and add/subtract them. So, each term is of the order of 1e+33 - 1e+36. If you subtract such a values to obtain the determinant, the expected accuracy is in the order of 1e+17 - 1e+20. Indeed, this corresponds with the values you get. Given the relative accuracy, you are not able to reach further to zero.
Note that if you scale your input matrices, i.e. divide it by 1e+11, the solutions are indeed the same, but the determinants are probably more what you would expect.
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..
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,'}}}_{',',');
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.