Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this experiment note that the questions call for the creation of a function M

ID: 2079594 • Letter: I

Question

In this experiment note that the questions call for the creation of a function M-file to solve the problem. As before your function M-files will have to include help files, version information, error detection and extensive comments. You will also have to avoid bad practice such as choosing meaningless names for variables and/or functions and embedding universal numbers into the code. The module notes discuss the algorithms, sic Newton-Raphson algorithm, Gaussian elimination (with total pivoting) and Gauss-Seidel which you are called upon to employ, but they do not fully indicate how to implement these algorithms as MATLAB function M-files.

3.1. Code-development problems Problem 1 Create a MATLAB function M-file to solve the system of nonlinear equations (1) using the Newton-Raphson method. (1) 0.5+cos(x)tanh(y) 0 There are many solutions. Your code must find all of the real solutions with xlying between 0 and 4n and y being positive. As well as the usual requirements of MATLAB function M-files you will need to pay particular attention to initiation and termination conditions. In each of the problems the system of linear equations to be considered is: 3.2x2 x 0.5x B0 2.1x, 2.2x2 7.5x 1.9x x5 2.5 (2) 9.6x, 3xa 1.2x, 5.5x 7.5 2x1 +x2 -5.7x3 -11x4 -xs J6.1. 3x, -3x3 -4x4 +10xs 2.6

Explanation / Answer

Equation(2): MATLAB CODE

clc;

clear all;

close all;

A=[0 3.2 0 1 -0.5; -2.1 2.2 7.5 -1.9 1; 9.6 3 0 1.2 5.5; 2 1 -5.7 -11 -1 ; 3 0 -3 -4 10];

B=[0;-2.5;-7.5;6.1;2.6];

%Given system of equations lookslike Ax=B then x=A^-1*B

x=inv(A)*B

OUTPUT:

x =
-0.8769
0.1328
-0.7266
-0.3407
0.1688

Equation (1) MATLAB CODE:

function [x,iter] = newtonm(x0,f,J)
% Newton-Raphson method applied to a
% system of linear equations f(x) = 0,
% given the jacobian function J, with
% J = del(f1,f2,...,fn)/del(x1,x2,...,xn)
% x = [x1;x2;...;xn], f = [f1;f2;...;fn]
% x0 is an initial guess of the solution
N = 1000; % define max. number of iterations
epsilon = 1e-10; % define tolerance
maxval = 10000.0; % define value for divergence
xx = x0; % load initial guess
while (N>0)
JJ = feval(J,xx);
if abs(det(JJ))<epsilon
error('newtonm - Jacobian is singular - try new x0');
abort;
end;
xn = xx - inv(JJ)*feval(f,xx);
if abs(feval(f,xn))<epsilon
x=xn;
iter = 100-N;
return;
end;
if abs(feval(f,xx))>maxval
iter = 100-N;
disp(['iterations = ',num2str(iter)]);
error('Solution diverges');
abort;
end;
N = N - 1;
xx = xn;
end;
error('No convergence after 100 iterations.');
abort;
% end function

function [f] = f2(x)
% f2(x) = 0, with x = [x(1);x(2)]
% represents a system of 2 non-linear equations
f1 = x(1)^7 - x(2)^2 - 1;
f2 = cos(x(1))*tanh(x(2))+0.5;
f = [f1;f2];
% end function

function [J] = jacob2x2(x)
% Evaluates the Jacobian of a 2x2
% system of non-linear equations
J(1,1) = 2*x(1); J(1,2) = 2*x(2);
J(2,1) = x(2); J(2,2) = x(1);
% end function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote