I wonder if there's a standard function in Julia for Matlab's mrdivide? x = B/A which would solve the system of linear equations x*A = B for x. As far as I have seen, the standard linalg package does not have it.
It just works. To solve x * A = B for x: (mrdivide)
B / A
To solve A * x = B for x: (mldivide)
A \ B
The \ symbol means divide from the left, and the / symbol means divide from the right, as in MATLAB. You are correct that / is not documented for some reason. I do not know why.
Related
Matlab Symbolic tool has a very annoying feature to me. It automatically convert to rational/decimal values.
Example 1
z = sym('z', 'real');
f = 3^(1/2)*z;
we get expected Matlab return f = 3^(1/2)*z
Example 2
z = sym('z', 'real');
f = 3^(1/3)*z;
Then I got crappy result f = (3247657313705851*z)/2251799813685248, not desired f=3^(1/3)*z
The reason for this can be found here. By default sym() use rational approximation to the numbers.
My question is, how to disable this feature? For example, how to get return f=3^(1/3)*z from the Example 2?
The documentation you link to tells you how to work around this by forcing numeric values to be computed symbolically. In this case, it's the 1/3 piece that can't be represented with sufficient precision in double, so you can do:
f = 3 ^ (sym(1)/sym(3)) * z;
I have a program that does some work to get a matrix w, which is 3(n+1) by 3(n+1). I have a vector fbar that is 3(n+1) by 1. I want to get the matrix that, when w is multiplied by it, gives fbar.
In mathematical notation, w * A = fbar. I have w and fbar, and I want A.
I tried to solve it with this command:
fsolve({seq(multiply(w, A)[i, 1] = fbar[i, 1], i = 1 .. 3*(n+1))})
but I don't understand the response Maple gave:
fsolve({2.025881905 A1[2,1]+7.814009150 A1[3,1]+...
-7.071067816 10^(-13) A1[3,1]-0.0004999999990
A1[4,1]-0.0007071067294 A1[5,1]-0.0004999999990 A1[6,1])
A3[6,1]=0},{A1[1,1],A1[2,1],A1[3,1],A1[4,1],A1[5,1],A1[6,1],A\
2[1,1],A2[2,1],A2[3,1],A2[4,1],A2[5,1],A2[6,1],A3[1,1],A3[2,1]\
,A3[3,1],A3[4,1],A3[5,1],A3[6,1]})
What does this mean, and how can I get a more meaningful answer?
You can do this directly with the LinearSolve functional from the LinearAlgebra package if w and fbar are defined as a matrix and vector respectively. The below code makes a reproducible example. Note that the solution of LinearSolve should be equal to x.
w := Matrix(<<1,2,3>|<4,5,6>|<7,8,10>>);
LinearAlgebra[ReducedRowEchelonForm](%); ## Full rank => 1 solution)
x := <1,2,3>;
fbar := w.x;
## Solve the equation w.x = fbar
LinearAlgebra[LinearSolve](w,fbar);
Assume that A, B and C are positive constants. I need to solve the following equation in symbolic form as W(A,B,C).
solve(x-(exp(log(2)*x*A)-B)*C==0)
Any help is appreciated.
Since the equation mixes polynomials and transcendentals, my first thought was simply that it may not have an analytical solution. However, the form reminded me of the Lambert W function (Wikipedia and Mathworks blog post), so I tried the variable substitution u = x/c+b which yields the simplified problem
solve(u == alpha * exp(beta * u) , u);
where alpha = exp(-log(2)*a*b*c) and beta = log(2)*a*c. Running this through Matlab gives me
>> syms alpha beta u
>> solve(u == alpha * exp(beta * u) , u)
ans =
-lambertw(0, -alpha*beta)/beta
which indeed showcases a Lambert W function call. With this knowledge, we can get the solution in terms of xsol like so:
syms a b c alpha beta x u xsol usol
usol = solve(u == alpha * exp(beta*u),u);
log2 = log(sym(2));
xsol = subs(c*(usol-b),[alpha,beta],[exp(-log2*a*b*c),log2*a*c]);
Let's make sure the Symbolic Engine didn't make a mistake by checking the analytical solution against a numeric one:
anum = rand();
bnum = rand();
cnum = rand();
xana = double(subs(xsol,[a,b,c],[anum,bnum,cnum]));
xnum = vpasolve(x-(exp(log2*x*anum)-bnum)*cnum,x);
fprintf('%15.10e\n%15.10e\n%15.10e\n',xana,xnum,xana-xnum);
which displays
8.0171933677e-02
8.0171933677e-02
1.7453177064e-19
Note: certain combinations of a, b, and c may result in an equation with no real solution. In such situations, the analytical solution may give a complex number whose veracity should be highly questioned.
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')
I'm trying to solve an equation using maxima 13.04.2 but the answer isn't what I expect.
Example:
y2=A2*cos(2*pi*f2*t+phase2) we know A2=.4,f2=6.4951,t=1, trying to find **phase2**
y2=.4*cos(2*pi*6.4951+phase2)
I tried to solve the y2 equation for phase2 in maxima but it got rid of the cos function
kill(all);
A:A; phase:phase; solve(A*cos(2*pi*f*t+phase)=0,phase);
The answer that came back was
I thought something like this was suppose to come back
y2 = A2×cos(2πf2t + φ2) ⇒
y2/A2 = cos(2πf2t + φ2) ⇒
arccos(y2/A2) = 2πf2t + φ2 ⇒
arccos(y2/A2) - 2πf2t = φ2
so I could then plug in the vales
A2 = 0.4, f2 = 6.4951, t = 1 and get the phase
Any ideas how to get maxima to get the correct format?
PS: Yes I know I can do it by hand but I have thousands of equations like this and I plan on using octave arrays to call maxima to solve them and bring the answers back into octave.
Well, it seems straightforward.
(%i1) e:A*cos(2*%pi*f*t + phi) = y;
(%o1) cos(2 %pi f t + phi) A = y
(%i2) solve (e, phi);
solve: using arc-trig functions to get a solution.
Some solutions will be lost.
y
(%o2) [phi = acos(-) - 2 %pi f t]
A
(%i3) subst ([A = 0.4, f = 6.4951, t = 1], %o2);
(%o3) [phi = acos(2.5 y) - 12.9902 %pi]
Couple of notes. (1) solve can solve this equation, which is good, but bear in mind that it is fairly limited in its capability, and you can probably make up other equations that it can't solve. There are some other options for solving equations in Maxima, but it general that is a weak area. (2) Maybe instead of switching back and forth between Maxima and Octave, you can just code equation %o2 in Octave and evaluate that for different values of the parameters.