Filter löschen
Filter löschen

Finding Set Of Answers For ONE Equation Involving SEVERAL Unknown Variables In a Discrete Data Zone

1 Ansicht (letzte 30 Tage)
Suppose this equation: F(x,y)=9x+7y-60=0
The range which include just discrete numbers is: -5<x<10 and -5<y<10
All the possible answers in that range are: [x1,y1]=[2,6] and [x2,y2]=[9,-3]
I tried this code to find the answer set but it didn't find anything:
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. Now I don't know how to modify my code and want you to help me.
Thanks to you All

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 19 Feb. 2014
The line "F=9.*x+7.*y-60;" should be replaced by:
F=9.*X+7.*Y-60;
The way you had it, it only inspected the 16 pairs (-5,-5), (-4,-4), ..., (10,10) instead of checking all 256 possible pairings of x and y.
Assuming by 'discrete' you mean 'integer', there are much faster ways of finding solutions to the above equation rather than this brute force method. Equations of this type are known as Diophantine equations.
  1 Kommentar
SooShiant
SooShiant am 20 Feb. 2014
Thanks Roger. Its about 3 days I'm thinking on it. You solved it by a simple change in a second :)
Can you say the other ways of solutions to this kinds of equations?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by