Assignment2
Assignment2
Assignment 2
Bhavya Garg
21ME02034
Question 1
Approach: The problem requires solving the equation f(x, y) = x – e(xy) with respect to the
symbolic variables x and y. The goal is to find the values of xxx that satisfy the equation,
which involves both algebraic and exponential terms. To approach this, we use MATLAB's
Symbolic Math Toolbox, which allows for symbolic computation rather than numerical
methods. This is essential for dealing with transcendental equations like this one, where
exact solutions may not be straightforward but can be expressed using special functions such
as the Lambert W function.
Explanation: In the code, we first define the symbolic variables x and y using syms. The
function f(x,y) is then created as a symbolic expression. Using the solve function, we solve
the equation x – e(xy) for x, treating y as a parameter. The solve function returns the
solution.Finally, the solution is displayed using disp(), giving us the symbolic expression for x
in terms of y.
Question2:
Approach: This problem revolves around solving the implicit equation f(x,y)=sin(x) + ye xy − 1,
in two variables. First, we need to create a function that represents this equation, and then
we move on to solving it using a numerical method.
(a): The first step is to create a function file that represents the equation f(x,y)=sin(x)
+yexy−1,. This is accomplished by defining a MATLAB function (f.m), f1(x, y), which takes two
inputs x and y, and returns the value sin(x) + yexy − 1.
(b): In this step, we evaluate the function at a specific point, namely f(0,1), and compute the
derivative with respect to x at this point. This is done using the symbolic differentiation
function diff. To get an idea of the function’s behavior, we visualize the function and its
derivative using surface plots. The range of x values can be obtained from the plot and from
the results of the function evaluation, allowing us to determine the region where solutions
are valid.
Range of x: The range of xxx can be determined from the plot, where the function behaves
in a stable and smooth manner. If the function's behavior becomes erratic or too steep
outside certain x-values, that may indicate where the function is no longer valid or where
numerical methods might fail to converge. From the plots Range of y = [-1,1], Range of x =
[0.2, 2].
(c): We use MATLAB's fzero function to solve for the value of y(1) for a given x=1. fzero
numerically approximates the root of the equation, starting from an initial guess for both y
and x. This method ensures that we can solve for y for each value of x where the equation is
valid.
(d): Using a loop, we iteratively solve for y at multiple values of xxx. For each iteration, the
previous y value serves as the initial guess for the next one, allowing us to compute the
sequence of values y(n) for various values of x. This is achieved using fzero in a loop,
iterating over a set of x values.
(e): Finally, we plot the relationship between y and x by plotting the values of y computed
from the loop against the corresponding x values. This plot will represent the solution to the
implicit equation f(x,y) = 0, showing how y varies as a function of x.
Results:
The output of f(0, 1) is computed and displayed. The derivative at the point f(0,1) with
respect to x is calculated.
The function and its derivative are plotted to give insight into their behaviour. From the plots
Range of y = [-1 1], Range of x = [0.2 2].
The fzero function finds the root for each x, and the loop iterates to compute multiple values
of y.
Finally, a plot of y versus x is produced to visualize the solution curve.
Question3:
Approach: (a): Plotting the Functions : Contour Plot: Both equations f(x,y) = 0 and g(x,y) = 0
are plotted on the same figure using contour. f(x,y)=0: Red curve. g(x,y)=0: Blue curve.
Solutions exist at the intersections of these curves, visible in the plot. Number of solutions
from observation = 4.
(c): Solving Symbolically: The symbolic solver computes exact solutions for x and y. Solve
returns symbolic solutions, which are then converted to numeric values using double for
better readability.
Output:
1. Graphical Solution: The contour plot shows the intersections of the red and blue
curves. These intersections show that 4 solutions of the system exist as shown above
in graph.
2. Symbolic Solution: Exact solutions for x and y are displayed. Numeric values are
provided for practical interpretation. These also indicate that the number of
solutions are 4.
Question 4: Approach:
(b) Meshgrid and PlotA meshgrid over a square region [−2,2] × [−2,2] is created. Zero-level
contours of f(x,y) and g(x,y) are plotted on the same figure. The intersections of red and blue
curves indicate solutions.
(c) Root Estimation: The number of intersections in the plot provides the number of roots i.e
2.
(d): Symbolic Solution: The vpasolve function is used to solve the system numerically.
Multiple initial guesses are provided to capture all possible solutions.
Output:
1. Plot: Red curve (f(x,y)=0). Blue curve (g(x,y)=0).Intersections indicate the solutions.
2. Number of Roots: 2 (Counted from the plot). And their approximate locations are
marked below.
3. Symbolic/Numeric Solutions: Solutions for x and y are displayed in a table format
confirming that numerically also number of roots are 2 for the given system of
equations.
Question5:
Approach:
(a) Define Functions f(x,y) and g(x,y) : We create the following function files (f1.m and g1.m)
for modularity:
(b) Meshgrid and Zero-Level Curves : To visualize solutions, we evaluate f(x,y) and g(x,y) on a
grid of points.
3. Plot the zero-level curves: Use contour with level [0] to plot where f(X,Y)=0 (blue) and
g(X,Y)=0 (red)
(c) Count Roots and Estimate Locations: Intersection Points: Solutions occur where the blue
and red curves intersect. The number of roots are 6, and there approximate locations are
shown in the figure below:
Define the system symbolically: Use syms x y in MATLAB to declare x and y as symbolic
variables. Define the equations f(x,y)=0 and g(x,y)= 0. Use MATLAB's solve function to solve
the equations exactly.
Interpret results: The symbolic solver provides exact solutions for (x,y) where the equations
are satisfied. These results can be compared with the estimates from the contour plot. We
can clearly see, the number of roots are 6, and are approximately equal to the locations of
intersection marked in the contour plot above.
Question 6:
Approach:
Calculating f(0,0):The value of f(0,0) is computed by substituting x=0 and y=0 into the
function. The code outputs the result: The value of f(0, 0) is: 0.
Zero-Level Curve: The zero-level curve is the set of points (x,y) where f(x,y)=0. A grid (mesh)
is created over the square domain [−1,1] × [−1,1]. The zero-level curve is plotted using
contour.
To verify if the zero-level curve represents a function y = g(x): The vertical line test is used: if
any vertical line intersects the curve at more than one point, it is not a function.
In the range shown (x∈ [−1,1]) the curve appears to pass the vertical line test
because no vertical line intersects the curve at more than one point. This means that,
within this range, y can be expressed as a single-valued function g(x).
Potential Issues Beyond the Range: If f(x,y) has more complex behavior outside the
plotted range (e.g., x > 1 or x < −1), the curve might fail the vertical line test.For
instance, here for a range of (-18,18), it fails the verticle line test, hence it doesn’t
represent y = g(x), but in the given [-1,1], y can be expressed as a single-valued
function g(x).
The partial derivative fy(x,y) of f(x,y) with respect to y, holding x constant is calculated using
symbolic differentiation by using ‘diff’. The value of f_y(0, 0) is: 1
This is done for x∈ [−1,5] using MATLAB's fsolve function, which numerically solves
nonlinear equations.
For each x, g(x) is computed by solving f(x,y)=0 with an initial guess of y=0. fsolve iteratively
adjusts y to make f(x,y)=0. The options parameter suppresses unnecessary output during the
process.
Plotting g(x): Finally, the values of g(x) are plotted against x. The resulting graph shows the
curve of g(x) for x∈ [−1,5]
The plot shows that g(x) is not a simple, single-valued function across the domain:
1. Near x=1, the sharp changes indicate potential non-uniqueness of the solution g(x) or
instability in the numerical computation of the root.
2. The regions of smooth behavior suggest well-defined relationships between x and
g(x)
Output: In x∈ [−1,1], g(x) is indeed a well-defined, single-valued function because the zero-
level curve does not exhibit branching or discontinuity. Outside this range, particularly near
x=1, g(x) becomes multi-valued due to the non-uniqueness of roots for f(x,y)=0.