Find set of answers for 4 variables in an equation.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear all,
I have an equation and the variables are in a specific range.
I am searching for a way to find all sets of answers for this equation.
Should I use loop or solve. Also I have "ln" which does not work with syms Any idea is appreciated.
For example:
% solving x=y*ln(z)+exp(t);
Syms x y z t
x=[0.01:0.1:2];
y=[0:0.1:1];
z=[0.0001:0.001:2];
t=[273:1:900];
[x,y,z,t] = solve(x-y*ln(z)+exp(t));
1 commentaire
Réponses (2)
Walter Roberson
le 23 Mai 2012
Your formula is not properly formed. Your x, y, z, and t all have different sizes, and are all row vectors. y*z cannot be computed: "*" is matrix multiplication and the second dimension of y does not match the first dimension of z. We also cannot interpret the "*" as meaning element-by-element operation as there are different numbers of elements in y and z.
I am tempted to suggest what you want is
[X,Y,Z,T] = ndgrid(x,y,z,t);
F = X - Y .* log(Z) + exp(T);
idx = find(F == 0);
[X(idx(:)), Y(idx(:)), Z(idx(:)), T(idx(:))]
However when we look further, we see that this is quite unlikely to have any solutions over the given ranges, as the inputs are all nice decimal values, but log(Z) and exp(T) are going to be transcendental values (i.e., as irrational as you can get within a finite storage number system) and it is improbable that every digit of the first transcendental function will just happen to match the digits of the second transcendental function to cancel everything out to give you a nice exact zero.
Consider the possibility that your inputs are over-specified, that you should be specifying only 3 of them and computing the 4th.
I ran the above computation, and sure enough there are no exact zeros at all. The minimum value computed is about 3.65 * 10^118. This is because of the exp(t) with quite large T. Your maximum y in the expression y*log(z) is 1, so you are looking for places where log(z) = -exp(t). This has a solution in z when z = exp(-exp(t)) and for t = 273 that is exp(-3.65E118) which is smaller than any number you are going to be able to represent in MATLAB or the Symbolic Toolbox.
I would suggest that your formula is probably incorrect.
5 commentaires
Walter Roberson
le 24 Mai 2012
If you show your actual function, then someone might be able to do some numeric analysis to find constraints on the values.
Before we get to far, please read
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
SooShiant
le 19 Fév 2014
Dear Walter Roberson
Thank you to guide us. I'm new to MATLAB and always have the same problem which Victor mentioned. Now i have a 12 variables function and need to find an answer set.
I used your code in a simple equation which know the answer set but it didn't work.
The equation is: F(x,y)=9x+7y-60=0
The range is: -5<x<10 and -5<y<10
The answer is: If F(x,y)=0 Then [x,y]=[[2,6][9,-3]]
Based on your guide, the way I defined the equation in MATLAB:
x=[-5:1:10];
y=[-5:1:10];
[X,Y]=ndgrid(x,y);
F=9.*x+7.*y-60;
idx=find(F==0);
[X(idx(:)),Y(idx(:))];
As below, If we change the range the way one of the answers come to first row, MATLAB find just that answer for us:
x=[2:1:10];
y=[6:1:14];
[X,Y]=ndgrid(x,y);
F=9.*x+7.*y-60;
idx=find(F==0);
[X(idx(:)),Y(idx(:))];
So the problem has found now. This code calculates F(xi,yj) just for the first row (i=1), so necessarily the only founded result is in the first row. Therefore we should improve our code the way counter calculates F1,1 to F16,16 then MATLAB search within all 16*16 elements of the F matrix. Due to my beginner skills in MATLAB I don't know how to form that matrix, otherwise it seems to be a simple problem for you experts.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!