# Obtain x and y variables for a given value of z which is a function of both

2 views (last 30 days)
Zhenteng Shen on 22 May 2020
Commented: Ameer Hamza on 22 May 2020
Hello all, thanks for looking.
For example if I have composite material consisting of concentrations of A and B and their resulting measured stress value is Z (in picture below this is the matrix in green). Now given the value of z, how do I find the value of A and B which correspond to it?
Usually the interp2 would work if I know A and B and want to find Z value, but now I want to do the opposite. The z value is beyond the range of results too, so may need to be extrapolated.
I currently have the script below whcih plots a surface sketch but I want to essentially find x and y values that correspond to a specific z value. I assigned the A concentration as x, B concentration as y and the stress value of thir combination z.
So as an exmaple, I want to know x and y values that would make z=0.4.
x=[2.5; 5]
y=[1; 1.5]
z=[4.9 5.3; 13.8 14.4]
surf(x,y,z) Stephen Cobeldick on 22 May 2020
"Now given the value of z, how do I find the value of A and B which correspond to it? "
Even if your surface is nicely continuous there can be zero, one, some finite number, or infinite solutions to this. Consider your example data, for example if we look for values where z=10 then this means all x and y values on the line from (2.5,1.2865) to (5,1.2582), i.e. infinite points. Which of those infinite points do you want as the output?
Zhenteng Shen on 22 May 2020
I see, I completely understand what you are saying now, Ameer demonstrated it graphically for me in my other question. Thank you for taking your time to comment.

Ameer Hamza on 22 May 2020
Edited: Ameer Hamza on 22 May 2020
As mentioned in the comment to your other question, if you try to extrapolate the surface that there is not a unique solution corresponding to a given z value. The following shows one of the ways; however, you can note that the solution is different every time you run it. It is because each initial guess to fsolve() will lead to a different solution.
x=[2.5; 5];
y=[1; 1.5];
z=[4.9 5.3; 13.8 14.4];
[X,Y] = meshgrid(x,y);
mdl = scatteredInterpolant(X(:), Y(:), z(:));
fun = @(x) mdl(x(1),x(2));
sol = fsolve(@(x) fun(x)-0.4, rand(1,2));
x_sol = sol(1);
y_sol = sol(2);

Zhenteng Shen on 22 May 2020
Perfect! Thanks a lot! :D
Zhenteng Shen on 22 May 2020
Hi Ameer, just an update I have found out which starting points to use in place of rand(1,2) for fsolve so will create a 1x2 matrix of the starting x value and y value to use. In this case your answer is exactly what I was looking for!!! I can't believe it, THANK YOU SO MUCH AGAIN!!!
Ameer Hamza on 22 May 2020
I am glad to be of help!