Simplify matrix matlab - matlab

I'm making some matrix computation in matlab. What looks strange (to me) is that I get results like
(8700286382685973*cos(q5)*sin(q4))/9007199254740992 + sin(q5)*((43220913799951902644522757965203*cos(q4))/730750818665451459101842416358141509827966271488 - 291404338770025/1125899906842624)
but matlab does not simplify the result. I already tried to use functions like simplify, simple,fix but none of them gave the desired result.
Any suggestion on what function should I use?

Simplify does only "exact" manipulations. What you need is a command that kills small terms in your expression. In Mathematica "Chop" takes care of that. Try to google it.

As #Lucas suggested, you can use vpa and digits in matlab, for example if the expression above is A (sym) then:
vpa(A,3) % digits is set to 3
ans =
0.966*cos(q5)*sin(q4) + sin(q5)*(5.91e-17*cos(q4) - 0.259)
And then you can either see the numbers for themselves and chop them, or use something like:
function result = significant(x, n)
% significant(x, n) rounds number x to n number of significant figures
s = floor(log10(abs(x)));
shift = 10^(n-1);
mant = round(x*shift/(10^s)) / shift;
result = mant * 10^s;

Try doing one of these commands before your evaluation:
format longe
format shorte

Related

How to combine fractions using octave symbolic?

Does anyone know how to get Octave to combine fractions in the symbolic package? For example, I would like to make "1+1/s" rewritten as "(s^2 + 1)/s".
My reason is that I want to get zeros and poles of frequency domain expressions into the right place in the rational expression. The above is a very simple example of a more complicated use case, typically with lots of R, L, C constants.
You can use the function factor(), in this case it will simply put the whole expression with the same common denominator:
syms x,s
x = 1+1/s
res = factor(x)
and
res =
s + 1
─────
s

Compute all the local extremepoints of a function using fminunc

So I want to compute the lokal extremas for the function
I used this link to help me out. Here is what I've done:
options = optimset('Display','off');
[min1,fval1]=fminunc(#(x)(sin(x(1)+x(2))+3*(x(1)-x(2)/2)^2)*exp(-(x(1)^2+x(2)^2)),[0;-1],options
[min2,fval2]=fminunc(#(x)(sin(x(1)+x(2))+3*(x(1)-x(2)/2)^2)*exp(-(x(1)^2+x(2)^2)),[1.18;2.3],options)
[max1,fval3]=fminunc(#(x)-(sin(x(1)+x(2))+3*(x(1)-x(2)/2)^2)*exp(-(x(1)^2+x(2)^2)),[1;0],options)
[max2,fval4]=fminunc(#(x)-(sin(x(1)+x(2))+3*(x(1)-x(2)/2)^2)*exp(-(x(1)^2+x(2)^2)),[2.3;1.18],options)
format long g
The points p1, p2, p3 and p4 were obtained from WolframAlpha.
The output in the command window is this:
min1 =
-0.3032
-0.5316
fval1 =
-0.50675
min2 =
1.1773
2.2344
fval2 =
-0.00043457
max1 =
0.91017
-0.29929
fval3 =
-1.5746
max2 =
-0.80798
0.61241
fval4 =
-1.2629
Questions:
For min2 I have fval2 = -0.00043457, which is correct. But I want it to say fval2 = -0.0004, how can I do this?
For max1 and max2, I just made the entire function negative and then applied fminunc. This gave me correct (x,y)-koordinates of the respective maxima but the value at these points are given with opposite signs. How can I make Matlab give me fval3 and fval4 as positive?
If you want to round fval2 to 4 decimal places, use round(fval2,4). If you don't want to round and simply want to display fval2 till 4 decimal places, use sprintf i.e. sprintf('%.4f',fval2).
Note that you are getting that many digits in the first place since you've changed the format. To reset it to default, enter format or format short.
You are meant to negate your objective function and calculate minimas of the negated function. These minimas are actually maximas of the original function but fval3 and fval4 are internally calculated by putting them in the negated function and hence you get the negative sign. Simply negate them again or evaluate your original function on the maximas.
Results from your Matlab script and Wolfrom are identical for p1 and p2 as they are minimums. (Once again see the Wolfram page)
For maximums, I found the problem. Your code is ok. Only the initial guesses should be better determined for p3 and p4. Use your own code but with these guesses for 3rd and 4th function:
h=#(x) -(sin(x(1)+x(2))+3*(x(1)-x(2)/2)^2)*exp(-(x(1)^2+x(2)^2));
[max1,fval3]=fminunc(h,[-1;1]);
[max2,fval4]=fminunc(h,[1;-0.3]);
However, I recommend you another method for finding local extermas: Find where the derivative of the function with respect to x and y is zero.
Q1: Use round() function to round it.
Q2: The answers Matlab displayed for this script are exactly identical to the Wolfram.
max1 =
-0.807979637093575
0.612410258594483
max2 =
0.910173217929548
-0.299286619005528
Q3: Use x=fminunc(...); instead of [x,fval]=fminunc(...); to omit fval.

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')

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 gamma function: I get Inf for large values

I am writing my own code for the pdf of the multivariate t-distribution in Matlab.
There is a piece of code that includes the gamma function.
gamma((nu+D)/2) / gamma(nu/2)
The problem is that nu=1000, and so I get Inf from the gamma function.
It seems I will have to use some mathematical property of the gamma
function to rewrite it in a different way.
Thanks for any suggestions
You can use the function gammaln(x), which is the equivalent of log(gamma(x)) but avoids the overflow issue. The function you wrote is equivalent to:
exp(gammaln((nu+D)/2) - gammaln(nu/2))
The number gamma(1000/2) is larger than the maximum number MATLAB support. Thus it shows 'inf'. To see the maximum number in MATLAB, check realmax. For your case, if D is not very large, you will have to rewrite your formula. Let us assume that in your case 'D' is an even number. Then the formula you have will be: nu/2 * (nu/2 -1) * ....* (nu/2 - D/2 + 1).
sum1 = 1
for i = 1:D/2
sum1 = sum1*(nu/2 - i+1);
end
Then sum1 will be the result you want.