Question

In: Computer Science

!!TRANSLATE TO MATLAB CODE!! INPUT: Function f, endpoint values a, b, tolerance TOL, maximum iterations NMAX...

!!TRANSLATE TO MATLAB CODE!!



INPUT: Function f, 
       endpoint values a, b, 
       tolerance TOL, 
       maximum iterations NMAX
CONDITIONS: a < b, 
            either f(a) < 0 and f(b) > 0 or f(a) > 0 and f(b) < 0
OUTPUT: value which differs from a root of f(x) = 0 by less than TOL
 
N ← 1
while NNMAX do // limit iterations to prevent infinite loop
    c ← (a + b)/2 // new midpoint
    if f(c) = 0 or (ba)/2 < TOL then // solution found
        Output(c)
        Stop
    end if
    NN + 1 // increment step counter
    if sign(f(c)) = sign(f(a)) then ac else bc // new interval
end while
Output("Method failed.") // max number of steps exceeded

Solutions

Expert Solution

This pseudo code is of Bisection method of root solving.

%%xNs column gives the answer.
clc;clear all;
f=@(x) x-sin(x); %%Solving for the root of x-sin(x)=0 as test case
a=0;b=2; imax=50;tol=0.01; %%Initial conditions
fa=f(a); fb=f(b);
if((fa*fb)>0) %%Checks if value has opposite signs then proceeds
fprintf('Function has the same sign at the points');
else
disp('Iteration a b xNS f(xNS) Tolerance');
for(i=1:imax)
xNS=(a+b)/2; %Mid point
toli=(b-a)/2; %Computes tolerance
FxNs=f(xNS);
fprintf('%3i %11.6f %11.6f %11.6f %11.6f %11.6f \n',i,a,b,xNS,FxNs,toli);
if(FxNs==0)
fprintf('Exact solution found at x=%11.6f',xNS);
break
end
if(toli<tol) %Breaks if tolerance level is reached
break;
end
if(i==imax)
fprintf('Solution not found in %i iterations',imax);
break
end
if(f(a)*FxNs<0)
b=xNS;
else
a=xNS;
end
end
end
  
  


Related Solutions

Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
In the function f(x) = 5x4-2x2-27 find the following: a) their critical values b)local maximum and...
In the function f(x) = 5x4-2x2-27 find the following: a) their critical values b)local maximum and minimum points c)The intervals where the concavity is up and down d) draw the graph and mark on it all the important points; maximum, minimum and inflection points
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). B) use...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). B) use Gauss-siedel iteration. C)use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given. * A=[4,8,0,0;8,18,2,0;0,2,5,1.5;0,0,1.5,1.75] , B=[8;18;0.5;-1.75]. , (optimal w is 1.634.)
Find the absolute maximum and minimum values for the function f(x, y) = xy on the...
Find the absolute maximum and minimum values for the function f(x, y) = xy on the rectangle R defined by −8 ≤ x ≤ 8, −8 ≤ y ≤ 8.
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). A)use Jacobi...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). A)use Jacobi iteration. B) use Gauss-siedel iteration. 1) make sure to use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given. * A=[1,-2,0,0;-2,5,-1,0;0,-1,2,-0.5;0,0,-0.5,1.25]] , B=[-3;5;2;3.5]. , (optimal w is 1.5431.)
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
1.) Find the local maximum and minimum values and saddle point(s) (x,y,f) of the function. If...
1.) Find the local maximum and minimum values and saddle point(s) (x,y,f) of the function. If you have three-dimensional graphing software, graph the function with a domain and viewpoint that reveal all the important aspects of the function. (Enter your answers as a comma-separated list. If an answer does not exist, enter DNE.) f (x, y) = xy − 5x − 5y − x2 − y2 2.)Find the local maximum and minimum values and saddle point(s)(x,y,f) of the function. If...
Find the absolute maximum and minimum values of the function f(x, y) = x^2 + ((4/3)...
Find the absolute maximum and minimum values of the function f(x, y) = x^2 + ((4/3) y^3) − 1 on the disk x^2 + y^2 ≤ 1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT