tricknology
tricknology

Reputation: 1138

How to use NSolve or Solve with lists for variables?

I'm trying to solve this expression:

NSolve[Qforced == hFree*SA*(Tskin - T), T]

but Qforced and hFree are lists of variables but same size, ex: {1,2,3,4...};

I am getting an empty solution "{ }" but if I do:

NSolve[Qforced[[1]] == hFree[[1]]*SA*(Tskin - T), T]

I get an answer. What I would like to do is this (written in Java):

ArrayList answerSets = new ArrayList<ArrayList<Double>>;
for (int i = 0; i < Qforced.size(),i++ ){
  ArrayList<Double> temp = new ArrayList<Double.
  for (int j = 0; k < hFree.size()){
    double Q = Qforced.get(i);
    double h = hFree.get(j);
    double T = -(Q/(h*SA)) - Tskin;
    temp.add(T);
  }
  answerSets.add(temp);
}

answerSets would be an arraylist of length Q.size(), each element being an arraylist of Q.size() containing the solution to the expression "T = -(Q/(h*SA)) - Tskin"

I would hate to write NSolve Qforced^2 times. I've come across this problem a lot and I can't seem to find any documentation that explains what I want to do. Any Ideas?

Thanks in advance!

Upvotes: 1

Views: 571

Answers (1)

Timo
Timo

Reputation: 4326

Do you want to solve the equation for all possible combinations of the indices? If so you can use Outer[] to avoid writing the equation N^2 times

Outer[NSolve[#1 == #2 * SA * (Tskin - T), T]&, Qforced, hFree]

If you need to pick out just certain parts of the output that NSolve gives you (in case there are multiple solutions), you can change NSolve[...]& to NSolve[...][[part]]& where part is a Part[] specificaltion.

Upvotes: 2

Related Questions