This semester we have already used the equation y = f(x) = 3x^3 - x as an exampl
ID: 3109769 • Letter: T
Question
This semester we have already used the equation y = f(x) = 3x^3 - x as an example to test root-finding methods. It is clearly easy to compute a value of y for a specified value of x. It is a bit more challenging to determine the value of x for a specified value of y, and the answer is not necessarily unique. For example, y = 0 for x = 0, for x = -1, and for x = +1. However, if y > 0 and the answer must be a positive value of x, there is a unique value of x. Although there are faster numerical methods for determining a good approximate value for x, perhaps the simplest is to solve the equation for x, and iterate. x = cubicroot y + x Using subscripts to indicate the first, second, third ... estimates: x_1 = cubicroot y; x_2 = cubicroot y + x_1; x_3 = cubicroot y + x_2; ... Craft a UDF that: Accepts a 2-element vector in the input argument list in which the 1^st element is a value for y that is greater than zero and the second element is a convergence threshold value such as 0.0001. Uses the numerical method described above to compute an approximate value for value of x corresponding to the specified value of y. Uses the following convergence criterion to terminate iteration tol greaterthanorequalto |1 - x_i - 1/x_i| Where tol = value specified in the input argument. Displays to the CW in a meaningful way the value computed for x.Explanation / Answer
matlab code
clc;
clear all;
close all;
a=[20,0.000001];
exam1(a);
function [] = exam1 ( a)
y=a(1);
tol=a(2);
x(1)=0;
for n=1:100
x(n+1)=(y+x(n))^(1/3);
fprintf('iteration %d : value of x = %f ',n,x(n+1));
if (tol >= abs(1-x(n)/x(n+1)))
break;
end
end
output:
iteration 1 : value of x = 2.714418
iteration 2 : value of x = 2.832048
iteration 3 : value of x = 2.836928
iteration 4 : value of x = 2.837130
iteration 5 : value of x = 2.837138
iteration 6 : value of x = 2.837139
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.