How to resolve an equation in dart (from a string) - flutter

I'm trying to resolve equations in dart. Here an example of an equation: y = x * 16. The y variable is a known value and x the value I want to compute. For example, if y = 32, I want to resolve the equation 32 = x * 16 (so 2).
I can't transform the equations manually because they come from an external source. Some examples : y = x * 16, y = (x + 35) / 1500, eventually y = (x + 14) - (x - 5). The left operand is always y (a known value). I can eventually simplify the right operand manually.
I tried to use the math_expressions and equations packages, but without success.

Related

initial point in CORDIC algorithm

I am trying to reduce number of iterations required to calculate multiplication using the CORDIC algorithm because I am using this algorithm in a continuous function to calculate square function. Here is the algorithm assuming -1<x<1'
function z=square(x)
y=x;
z=0;
for i=1:15
if (x > 0)
x = x - 2^(-i);
z = z + y*2^(-i);
else
x = x + 2^(-i);
z = z - y*2^(-i);
end
end
return
end
I already know the close value to multiplication result (from the previous result (call it pr)) and value of x (the value of x is continuous) . Does it help in anyway to decrease number of iterations?
If you are multiplying twice by the same constant, say a.x and a.x', then you can multiply and add with the delta a.(x' - x), which has less significant digits.
In case both factors vary, you can still use
x'.y' = (x'- x).(y' - y) + x.(y' - y) + (x' - x).y + x.y
where maybe the first term is neglectible.
For a square,
x'² = (x'- x)² + 2.x.(x' - x) + x²

The function "triplequad" doesn't return the right volume (Octave/Matlab)

I am trying to get the volume under a profile by using Octave. I have built a simple model for this:
function F1 = f_x(x)
F1 = x-1;
endfunction
function F2 = f_y(y)
F2 = y;
endfunction
function F3 = f_z(z)
F3 = z;
endfunction
f_xyz = inline('f_x(x).*f_y(y).*f_z(z);', 'x', 'y', 'z');
Volume = triplequad(f_xyz,0,3,0,3,0,4)
x = 1:1:2;
y = 1:1:2;
z = 1:1:2;
f_plot=f_x(x).*f_y(y).*f_z(z)';
%Lines for plotting the 3D plot
tx = linspace (0, 3, numel(x))';
ty = linspace (0, 3, numel(y))';
[xx, yy] = meshgrid (tx, ty);
tz = f_plot;
mesh (tx, ty, tz);
which gives a plot that looks like in the picture below:
I am using the triplequad function (which works also on Matlab) to get the volume under that profile, but it doesn't look like it works. The function return a volume of 54 units, which is not really true. Calculating the volume of a parallelepiped using the full dimensions gives 36 units (3 x 3 x 4), which proves that it calculates it wrongly. The questions is... what am I doing wrong here? Why doesn't "triplequad" give the right volume?
I think you might be misunderstanding the dimensionality of your problem. You're dealing with a 4D function:
f = #(x, y, z) (x-1).*y.*z;
You have a value returned by f (i.e. the dependent variable) determined by a set of 3 independent variables, thus a 4D function. You have to talk in terms of hypervolume instead of volume in this case, since volume is a 3D measurement. The triplequad function will evaluate a triple integral of f over a given set of ranges for the independent variables, giving you the hypervolume:
>> triplequad(f, 0, 3, 0, 3, 0, 4) % Integrate over 0 <= x <= 3, 0 <= y <= 3, 0 <= z <= 4
ans =
53.999999999999986 % Just shy of 54
Your visualization of a 3D surface doesn't make any sense, and is leading you astray in thinking that the result is wrong.
NOTE 1: In MATLAB, the function triplequad appears slated for deprecation in a future release, so you should use integral3 instead. It also appears to give more accurate results:
>> integral3(f, 0, 3, 0, 3, 0, 4)
ans =
54
NOTE 2: In regards to your comment, the function f(x,y,z) = 1 is constant and has no dependence on x, y, or z. A triple integral over this function is the equivalent of computing the volume of the area over which you're integrating (3*3*4 = 36) multiplied by the constant function value (which is just 1 in this case). You can confirm it like so:
>> f = #(x, y, z) ones(size(x));
>> triplequad(f, 0, 3, 0, 3, 0, 4)
ans =
36
EDIT: Regarding your follow-up problem, I actually get an error when trying to run that example in R2016b, so I'm not sure how you got it to work (although the resulting value is correct):
>> V = triplequad(h_xxyy,0,3,0,3,0,15)
Error using inline/subsref (line 14)
Too many inputs to inline function.
The problem is that triplequad is used to perform triple integrals over functions of 3 variables, but your function only has 2 inputs (x and y). When calculating the volume under a function of 2 variables, you only need to integrate over those 2 variables, so you should be using the MATLAB function integral2 instead (or dblquad in Octave):
>> f = #(x, y) x.^2.*sqrt(y);
>> V = integral2(f, 0, 3, 0, 3)
V =
31.176914536244894
Note that f has no z-dependence, so f(x, y) is constant with respect to z (i.e. for a given x and y, it always returns the same value regardless of z). As a result, if you were to perform a third integration over the z dimension it would be the same as multiplying the result by the integration range:
>> V*15
ans =
467.6537 % Same result you got from triplequad

Matlab : Is the square operation in real domain the Conjugate operation in complex domain?

I am trying to evaluate the expression z = (x-y)^2 in real domain and its corresponding adaptation in complex domain. For real domain, this expression is implemented as
let
x = 5;
y = 2;
z = (x-y)^2
z =
9
In complex domain, the expression would become (please correct me if wrong )
z_c = (x_c - y_c)(x_c - y_c)* This is implemented in Matlab by
>> x_c = 5 + 0.9i;
y_c = 2 - 0.34i;
z_c = (x_c-y_c)*conj((x_c -y_c))
z_c =
10.5376
The * operator for conjugate in maths is implemented by conj()
The answers are different and am I using the correct operator?
You have many ways to deal with that in MATLAB:
x = 5 + 2i;
y = 2 - 4i;
% Method A
(x - y) * conj(x - y);
% Method B
(x - y)' * (x - y);
% Method C
norm(x - y, 2) ^ 2;
The first method is using the Conjugate Operator.
This method is written assuming both x and y are scalar.
Method B is using the definition of Inner Product (The ' is the Vector Adjoint Operator - Transpose and Conjugate).
It will work for vectors as well.
Method C is using the built in norm() function of MATLAB.
Enjoy.

Series expansion of a function about infinity - how to return coefficients of series as a Matlab array?

This question is connected to this one. Suppose again the following code:
syms x
f = 1/(x^2+4*x+9)
Now taylor allows the function f to be expanded about infinity:
ts = taylor(f,x,inf,'Order',100)
But the following code
c = coeffs(ts)
produces errors, because the series does not contain positive powers of x (it contains negative powers of x).
In such a case, what code should be used?
Since the Taylor Expansion around infinity was likely performed with the substitution y = 1/x and expanded around 0, I would explicitly make that substitution to make the power positive for use on coeffs:
syms x y
f = 1/(x^2+4x+9);
ts = taylor(f,x,inf,'Order',100);
[c,ty] = coeffs(subs(ts,x,1/y),y);
tx = subs(ty,y,1/x);
The output from taylor is not a multivariate polynomial, so coeffs won't work in this case. One thing you can try is using collect (you may get the same or similar result from using simplify):
syms x
f = 1/(x^2 + 4*x + 9);
ts = series(f,x,Inf,'Order',5) % 4-th order Puiseux series of f about 0
c = collect(ts)
which returns
ts =
1/x^2 - 4/x^3 + 7/x^4 + 8/x^5 - 95/x^6
c =
(x^4 - 4*x^3 + 7*x^2 + 8*x - 95)/x^6
Then you can use numden to extract the numerator and denominator from either c or ts:
[n,d] = numden(ts)
which returns the following polynomials:
n =
x^4 - 4*x^3 + 7*x^2 + 8*x - 95
d =
x^6
coeffs can then be used on the numerator. You may find other functions listed here helpful as well.

constant term in Matlab principal component regression (pcr) analysis

I am trying to learn principal component regression (pcr) with Matlab. I use this guide here: http://www.mathworks.fr/help/stats/examples/partial-least-squares-regression-and-principal-components-regression.html
it's really good, but I just cannot understand one step:
we do the PCA and the regression, nice and clear:
[PCALoadings,PCAScores,PCAVar] = princomp(X);
betaPCR = regress(y-mean(y), PCAScores(:,1:2));
And then we adjust the first coefficient:
betaPCR = PCALoadings(:,1:2)*betaPCR;
betaPCR = [mean(y) - mean(X)*betaPCR; betaPCR];
yfitPCR = [ones(n,1) X]*betaPCR;
How come that the coefficient needs to be 'mean(y) - mean(X)*betaPCR' for the constant one factor? Can you explain that to me?
Thanks in advance!
This is really a math question, not a coding question. Your PCA extracts a set of features and puts them in a matrix, which gives you PCALoadings and PCAScores. Pull out the first two principal components and their loadings, and put them in their own matrix:
W = PCALoadings(:, 1:2)
Z = PCAScores(:, 1:2)
The relationship between X and Z is that X can be approximated by:
Z = (X - mean(X)) * W <=> X ~ mean(X) + Z * W' (1)
The intuition is that Z captures most of the "important information" in X, and the matrix W tells you how to transform between the two representations.
Now you can do a regression of y on Z. First you have to subtract the mean from y, so that both the left and right hand sides have mean zero:
y - mean(y) = Z * beta + errors (2)
Now you want to use that regression to make predictions for y from X. Substituting from equation (1) into equation (2) gives you
y - mean(y) = (X - mean(X)) * W * beta
= (X - mean(X)) * beta1
where we have defined beta1 = W * beta (you do this in your third line of code). Rearranging:
y = mean(y) - mean(X) * beta1 + X * beta1
= [ones(n,1) X] * [mean(y) - mean(X) * beta1; beta1]
= [ones(n,1) X] * betaPCR
which works out if we define
betaPCR = [mean(y) - mean(X) * beta1; beta1]
as in your fourth line of code.