my "lin" variable is a function of 2 variables: alpha and p1x.

   ptes = Solve[{x1, y1} \[Element] 
    lin[alpha0, p1x0] && {x1, y1} \[Element] rec1, {x1, y1}]

At the moment I perform the whole following operation for alpha=alpha0 and p1x=p1x0, saving the variables as such:

soltes = {x1, y1} /. ptes;
{p1xtes, p1ytes, p2xtes, p2ytes} = Flatten[soltes];

Dtes = ((p1xtes - p2xtes)^2 + (p1ytes - p2ytes)^2)^0.5

I have been making a few attempts at modifying the code so that Dtes remains a variable of alpha and p1x, without the slightest success. I am not sure on how to modify the code to carry on the variables dependence into Dtes, in other words to obtain Dtes[alpha_,p1x_]:= something[alpha,p1x]


3 Answers 3

ptes[alpha_, p1x_] = Solve[{x1, y1} \[Element] lin[alpha, p1x] && {x1, y1} \[Element] rec1, {x1, y1}]

soltes[alpha_, p1x_] = {x1, y1} /. ptes[alpha, p1x];

Dtes[alpha_, p1x_] = Norm[soltes[alpha, p1x][[1]] - soltes[alpha, p1x][[2]]];
  • $\begingroup$ thanks, I used your answer as a first step to elaborate my solution. It is certainly not elegant, produces a lot of warnings, and I still do not understand it fully, but it returns the results I needed. I'll try to explain what I did in an answer to my post. $\endgroup$
    – saimon
    Commented Apr 17, 2019 at 21:37

If I understand your intentions correctly, I think Module function could be of use for you here. Perhaps something like this should work:

ptes = Solve[{x1, y1} \[Element] lin[\[Alpha], p1x] && {x1, y1} \[Element] rec1, {x1, y1}];
soltes = {x1, y1} /. ptes;  
{p1xtes, p1ytes, p2xtes, p2ytes} = Flatten[soltes];
((p1xtes - p2xtes)^2 + (p1ytes - p2ytes)^2)^0.5
  • $\begingroup$ I do not really understand the logic in there, but I have tries pasting this chunk of code into my own: I get no output, and 2 errors: (1) Syntax::tsntxi: "(*<<1>>" is incomplete; more input is needed. (2) Syntax::sntxi: "Incomplete expression; more input is needed \!(\"\")" $\endgroup$
    – saimon
    Commented Apr 17, 2019 at 14:09
  • $\begingroup$ I'm not sure why that is, the above is just an example. I've used the functions that you have provided. I cannot validate my answer here because I don't know what your 'lin' function is, etc. As far as I'm concerned, the structure of the function I posted is correct. $\endgroup$
    – amator2357
    Commented Apr 17, 2019 at 14:23

Thanks to Roman I was able to find a very clunky solution: - I had to switch from Solve to Reduce, cause for some reason Solve did not work when my line intersected the vertical edges of my rectangle - I looked for a way to access Reduce solutions, that, of course, was not the same that worked with Solve that Roman suggested - I redefined the distance since Norm could not work in the way I stored variables - I quieted the wheelbarrow of warnings that I got, despite obtaining the correct result - please refrain from laughing to hard at my solution, I am just a casual mathematica user

(*distance in the first rectangle*)
Quiet[ptes[alpha_, p1x_] = 
   Reduce[{x1, y1} \[Element] lin[alpha, p1x] && {x1, y1} \[Element] 
      rec1, {x1, y1}]];

Quiet[soltes[alpha_, p1x_] = {x1, y1} /. ptes[alpha, p1x]];

x1tes[alpha_, p1x_] := soltes[alpha, p1x][[2]][[1]][[1]][[2]];
x2tes[alpha_, p1x_] := soltes[alpha, p1x][[2]][[2]][[1]][[2]];
y1tes[alpha_, p1x_] := soltes[alpha, p1x][[2]][[1]][[2]][[2]];
y2tes[alpha_, p1x_] := soltes[alpha, p1x][[2]][[2]][[2]][[2]];

    p1x_] := ((x1tes[alpha, p1x] - 
         x2tes[alpha, p1x])^2 + (y1tes[alpha, p1x] - 
         y2tes[alpha, p1x])^2)^0.5];

  Dtes[alpha, p1x], {{alpha, alpha0}, 0.001, 
   90}, {{p1x, p1x0}, -Dxmax + 1, Dxmax}]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.