explict (pointwise) function from numerical solution to implicit equation

I can plot the numerical solutions to a complicated expression f(x,y)=0 over intervals for x and y using fimplicit(f,interval). However, how do I then define/specify an explicit relation y=g(x) pointwise using the numerical values computed from f(x,y)=0 for use in a separate expression? (There is a functional relation.)

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 6 Juin 2020
Modifié(e) : Ameer Hamza le 6 Juin 2020
Try interp1 to create an explicit relation for 'y' in term of 'x' using the datapoints from fimplicit
f = @(x, y) y - x.^2;
fp = fimplicit(f, [-2 2 0 10]);
x = fp.XData;
y = fp.YData;
g = @(xq) interp1(x, y, xq);
Then run
>> g(1)
ans =
1.0002
>> g(0.5)
ans =
0.2501
Alternate solution:
Use fsolve() directly. The advantage is that you can control the spacing of x-points
f = @(x, y) y - x.^2;
x = -2:0.01:2;
y = fsolve(@(y) f(x, y), rand(size(x)));
g = @(xq) interp1(x, y, xq);

5 commentaires

The first method worked! (I had a problem applying the alternate solution in my case.) Thank you very much!
Laurence's comment posted as answer moved here:
Ameer,
As I said, your first suggestion worked. (Thank you again!) However, I am now trying to generalize this method verbatim to the case of three variables and am having trouble. My attempt is included below. Even if the 3-D commands are not well-defined, it is clear what I am attempting to do. That is, I am trying to solve f(x,y,z)=0 pointwise for an explicit function z=g(x,y), for which I can then store the data and use in the expression for dtdp, below. Do you have any suggestions?
Laurence
f = @(x,y,z)(2.*sqrt(z).*(1+sqrt(z)).*((y+sqrt(z).^2).*((8.*(1+x))./((1-x).*(3+x).^2))+((8./9).*(1-y).^2)+(2.*(y+sqrt(z)).*(1-y).*(8./(3-x).^2))))-((1-z).*(1-y).*((y+sqrt(z)).*(8.*(1+x)./((1-x).*(3+x).^2))-((1-y).*(8./9))+((1-y).*(8./((3-x).^2)))-((y+sqrt(z)).*(8./((3-x).^2)))));
interval = [0.0001 .9999 0.0001 .9999 0.0001 .9999];
fp = fimplicit3(f, interval);
x = fp.XData ;
y = fp.YData ;
z = fp.ZData ;
g = @(xq, yq) interp2(x, y, z, Xq, Yq );
dtdp = (((1-z).*(1-y))-((1-z).*(y+sqrt(z)))-(4.*sqrt(z)).*(1+sqrt(z)).*(y+sqrt(z)).*(((8.*(1+x))./((1-x).*((3+x).^2)))-(8./((3-x).^2)))-(2.*(1-z).*(1-y))-((1-z)+(4.*sqrt(z)).*(1+sqrt(z)).*(1-y)).*((8./((3-x).^2))-(8./9)))./((((1+sqrt(z))./(sqrt(z))).*((y+sqrt(z)).^2).*((8.*(1+x))./((1-x).*((3+x).^2)))+((y+sqrt(z)).^2).*((8.*(1+x))./((1-x).*((3+x).^2)))+((2.*(1+sqrt(z)).*((y+sqrt(z))).*((8.*(1+x))./((1-x).*((3+x).^2)))+((1-y)).*(y+sqrt(z)).*((8.*(1+x))./((1-x).*((3+x).^2)))-(((1-z)./(2.*(sqrt(z))))).*(1-y).*((8.*(1+x))./((1-x).*((3+x).^2)))+(((1+sqrt(z))./(sqrt(z))).*(1-y).^2).*(8./9)+(((1+sqrt(z))./(sqrt(z)))).*2.*(y+sqrt(z)).*(1-y).*(8./((3-x).^2))+((y+sqrt(z)).*(1-y))*(8./((3-x).^2))+(2.*(1+sqrt(z)).*(1-y))*(8./((3-x).^2))+((y+sqrt(z)).*(1-y))*(8./((3-x).^2))+((1-y).^2).*(8./((3-x).^2))+((2.*(1+sqrt(z)).*((y+sqrt(z))).*(8./((3-x).^2))))))));
plot3 (x,y,dtdp);
xlabel('alpha')
ylabel('po')
zlabel('dtdp')
You can do something like this in 3D case
x = fp.Triangulation.Points(:,1);
y = fp.Triangulation.Points(:,2);
z = fp.Triangulation.Points(:,3);
g = @(xq, yq) griddata(x, y, z, xq, yq );
There seems to be some issue in the equation of dtdp. See if it is written correctly.
That worked. Thanks again!!
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by