Question

In: Electrical Engineering

script for Secant Method. Using Matlab or Octave. The data: y = x.^2 - 1.2 x_lower...

script for Secant Method. Using Matlab or Octave.

The data:

y = x.^2 - 1.2

x_lower = 0.4

x_upper = 0.6
Es = 0.5*10^(2-n)

n = 5

Solutions

Expert Solution

Matlab Program-1:

a=input('Enter given function:','s');
f=inline(a)

x(1)=input('Enter the first point of guess interval: ');
x(2)=input('Enter the second point of guess interval: ');
n=input('Enter the allowed Error in calculation: ');
iteration=0;

for i=3:1000
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
iteration=iteration+1;
if abs((x(i)-x(i-1))/x(i))*100<n
root=x(i)
iteration=iteration
break
end
end

Result :

Enter function:x.^2 -1.2

f =

Inline function:
f(x) = x.^2 -1.2

Enter the first point of guess interval: 0.4
Enter the second point of guess interval: .6
Enter the allowed Error in calculation: 0.5*10^-3 ( Es = 0.5*10^(2-n) where n = 5

root =

1.0954


iteration =

6

or

Matlab program- 2:

function secant_method()
f = @(x) x^2 - 1.2;
n=5;
eps = 0.5.*10^(2-n);
x0 =0.6; x1 = x0 - 0.001;
[solution,no_iterations] = secant(f, x0, x1, eps);
if no_iterations > 0 % Solution found
fprintf('Number of function calls: %d\n', 2 + no_iterations);
fprintf('A solution is: %f\n', solution)
else
fprintf('Abort execution.\n')
end
end

function [solution,no_iterations] = secant(f, x0, x1, eps)
f_x0 = f(x0);
f_x1 = f(x1);
iteration_counter = 0;
while abs(f_x1) > eps && iteration_counter < 100
try
denominator = (f_x1 - f_x0)/(x1 - x0);
x = x1 - (f_x1)/denominator;
catch
fprintf('Error! - denominator zero for x = \n', x1)
break
end
x0 = x1;
x1 = x;
f_x0 = f_x1;
f_x1 = f(x1);
iteration_counter = iteration_counter + 1;
end
% Here, either a solution is found, or too many iterations
if abs(f_x1) > eps
iteration_counter = -1;
end
solution = x1;
no_iterations = iteration_counter;
end

Result:

Number of function calls: 6
A solution is: 1.095563


Related Solutions

Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it in the variable fMultiplier. The script should them prompt the user for a number, storing it in the variabl fIn. The script should then calculate the product of these two variables, saving the result to another variable fOut, and printing it. The script should then repeat the prompt for fIn, calculation, and output twice more, using the same variable fIn and fOut all three...
Using MATLAB or Octave, Write a script that prompts the user for a minimum and maximum...
Using MATLAB or Octave, Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer value: 20 A random...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters) performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken. The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is...
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
Solve the following equations using the Newton-Raphason method. ( using matlab) x^2+x*y^2 = 9 ?3x^2 *...
Solve the following equations using the Newton-Raphason method. ( using matlab) x^2+x*y^2 = 9 ?3x^2 * y - y^3 = 4    ?initial estimation of (x,y) = (1.2, 2.5) ?please help.. using matlab and matlab code
Using matlab Find x and y that solve the following system: ln(x 2 + y) =...
Using matlab Find x and y that solve the following system: ln(x 2 + y) = 1 − y , xy = − √ x
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method...
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method to locate the solutions with tolerance 10^(−10).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT