Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am struggling figuring out why my code is encountering these issues Consider the following systems of equations: y = - x 2 - A

I am struggling figuring out why my code is encountering these issues
Consider the following systems of equations:
y=-x2-Ax+0.75
y+Bxy=x2
where constants A and B are parameters. Develope a function file that will solve for x and y given the values of the two parameters in the system using the Newton-Raphson iteration. Your function should accept the following inputs (in order):
Ascalar value for A
A scalar value for B
A column vector of initial guesses for x0 and y0
A stopping critierion for the iteration
Your function should return the following outputs (in order):
A 2-element column vector of the solutions x and y
The Jacobian matrix evaulated at the initial guesses x0 and y0
The Euclidean norm for the residuals vector associated with the solution.|
The number of iterations required for covergence with default maximum at 50.
Note: Use an analytical formulation of the Jacobian matrix and do not rearrange the equations before computing partial derivatives if you want your code to pass the tests. You can use the referenced NRsys.m in your solution code.
Function o.
function , norm_res, iter]= P1_solver(A,B,, es )
Here is my current code:
function [xy, J, norm_res, iter]= P1_solver(A, B, xi, es)
% Initialize the Jacobian at current guess
J = ones(2,2);
J(1,1)=-(2*xi(1))- A;
J(1,2)=-1;
J(2,1)=(2*xi(1))-(B*xi(2));
J(2,2)=-1-(B*xi(1));
% Define the system of equations
f = @(x,y)-x^2- A*x +0.75- y;
g = @(x,y) x^2- y - B*x*y;
max_iter =50;
x_old = xi;
for iter =1:max_iter
% Evaluate the function at current guess
F_val =[f(x_old(1), x_old(2)); g(x_old(1), x_old(2))];
% Update the Jacobian at current guess
J(1,1)=-(2*x_old(1))- A;
J(1,2)=-1;
J(2,1)=(2*x_old(1))-(B*x_old(2));
J(2,2)=-1-(B*x_old(1));
% Compute update step using Newton-Raphson method
delta_x =-J\F_val;
% Update estimate of root
x_new = x_old + delta_x;
% Check convergence criterion (Euclidean norm of residual vector)
norm_res = norm(F_val);
if norm_res es || all(abs(delta_x) es*abs(x_new))
break;
end
if iter == max_iter && norm_res >= es
warning('Maximum iterations reached without achieving desired tolerance.');
end
% Update initial guess for next iteration
x_old = x_new;
end
xy = x_new;
end
Here are my current errors:
Assessment: 3 of 5 Tests Passed
Does the initial Jacobian matrix have the correct values for the example function inputs? (Pretest)
Variable J has an incorrect value.
Are the outputs correct for input values not given in the example?
Variable J has an incorrect value.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions