I can't find the the right code to draw a plot along the constraint

1 view (last 30 days)
hi everyone, I really need an answer
i have a function with a constrint function and I need to define the constraint function g; my problem is that i can't manage to write the right fplot3 in line 19 for the domain in the graph
clear
syms x y t l
assume([x y t l], 'real')
f(x,y) = x*y
fsurf(f,[0 5])
view(-20,10)
title('Graph of f(x,y) = xy')
xlabel('x')
ylabel('y')
zlabel('z = f(x,y)')
g(x,y) = x^2 +y^2 -4*x*y -1
constr = g(x,y) == 0
fsurf(f,[0 5])
hold on
fimplicit3(constr,'EdgeColor','none','FaceAlpha',0.8)
%--
%this is the line
fplot3(t, 5-t, f(t,5-t),'r--', [0 5], 'LineWidth', 2)
%--
title('Graph of f and of its restriction on the constraint')
xlabel('x')
ylabel('y')
zlabel('z = f(x,y)')
hold off
the dotted lines should coincide with the hyperbola but i can't manage to wrap my head around it. If anyone knows the answer pls help c:

Answers (1)

Gyan Vaibhav
Gyan Vaibhav on 11 Oct 2023
Edited: Gyan Vaibhav on 11 Oct 2023
Hi Tommaso,
The issue you're encountering is because you're trying to plot the function along the line "y = 5 - t", which doesn't seem to be related to your constraint.
The constraint “g(x,y) == 0” is a hyperbola in the x-y plane. To plot the function “f(x,y) = x*y” restricted to this constraint, you need to express “y” in terms of “x” from the constraint equation, and then substitute this into the function “f(x,y)”.
You can solve the constraint equation “g(x,y) == 0” for “y” to get two possible solutions (branches of the hyperbola). Then, create two new functions “y1(t)” and “y2(t)” for each branch of the hyperbola by substituting “x” with “t” in the solutions. Finally, use these functions in “fplot3” to plot the function “f(x,y) = x*y” along these lines.
Here's the updated part of your code:
g(x,y) = x^2 +y^2 -4*x*y -1;
sol = solve(g == 0, y); % solve constraint for y
y1(t) = subs(sol(1), x, t); % create function for first branch
y2(t) = subs(sol(2), x, t); % create function for second branch
fplot3(t, y1(t), f(t,y1(t)),'r--', [0 5], 'LineWidth', 2) % plot first branch
fplot3(t, y2(t), f(t,y2(t)),'r--', [0 5], 'LineWidth', 2) % plot second branch
Now the dotted lines should co-incide, as expected.
Hope this helps.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!