필터 지우기
필터 지우기

How can I let Matlab to put different numbers in different variables?

조회 수: 6 (최근 30일)
Ali Alnemer
Ali Alnemer 2017년 7월 14일
댓글: Star Strider 2017년 7월 15일
I have to complete my code that should solve an equation with 7 variables to get a specific number.
my equation is like this:
5*x1 + 6*x2 + 7*x3 + 8*x4 + 9*x5 + 10*x6 + 11*x7 = 77 (to get more answers, I will put range like = from 75 to 78).
I will get many solutions, but I am confused how to code it.
I started with initiating x1,2,3,4,5,6,7 and each time I make loop for changing one variable and keep other constants. Then, I do the same thing with the second variable.
This way is not efficient. Can you suggest a coding way that is more efficient?
Thank you
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 7월 14일
Are there constraints on the values of the variables? For example are they restricted to positive integers? If they are restricted to integers then you would have a Diophantine Equation and there are techniques for solving those.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2017년 7월 14일
You have 1 equation in 7 unknowns, so you will get an infinity of solutions. Solving it seems to be pointless.
To code it, I would use an anonymous function:
f = @(x) 5*x(1) + 6*x(2) + 7*x(3) + 8*x(4) + 9*x(5) + 10*x(6) + 11*x(7) - 77;
Your x-values are now one vector: [x(1),x(2),...,x(7)]. Given a random starting vector, you could use the fsolve function to ‘solve’ it for one set of variables. Whether that is an efficient use of your time is for you to decide.
  댓글 수: 8
Ali Alnemer
Ali Alnemer 2017년 7월 15일
But is there anyway to write a code (with loops for example) to give me matrix of some possible solutions ?
This is because I don't want to see only one solution. Instead, I want to see matrix of possible solutions ( so that if I put one solution in one of the equations, it gives me an answer even with range +-5 to the shown answer. For example, if I got one possible answer from only equation 1 and try to put this solution in the second equation, it gives 73.9 +- 5) and same for following equations.
Hope it is clear.
Star Strider
Star Strider 2017년 7월 15일
There is only one optimal solution to a linear system of seven (or more) equations with seven unknowns, with real coefficients. You can use the Statistics and Machine Learning Toolbox regress function to get the confidence intervals of the ‘X’ values, since there will be some uncertainty in their estimation.
You can use a loop to experiment with other values for the ‘X’ vector. However, the result the least-squares solution will be the best estimate for it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by