Question

In: Advanced Math

Starting from mynewton write a function program mysymnewton that takes as its input a symbolic function...

Starting from mynewton write a function program mysymnewton that takes as its input a symbolic function f and the ordinary variables x0 and n. Let the program take the symbolic derivative f ′ , and then use subs to proceed with Newton’s method. Test it on f(x) = x 3 − 4 starting with x0 = 2. Turn in the program and a brief summary of the results

Solutions

Expert Solution

code for matlab is
syms x
f=@(x) x^3-4;
f'=diff(f(x))
h=@(y) subs(f',y)

code for mathematica

f[x_]:=x^3-4

F'[X]                     (*THEN PRESS SHIFT AND ENTER*)

(TO GET THE VALUE FOR XO=2)

F'[2]                (*THEN PRESS SHIFT AND ENTER*)

AND SAVE PROGRAME aS mysymnewton







function [ x, ex ] = newton( f, df, x0, tol, nmax )
%
% NEWTON Newton's Method
%   Newton's method for finding successively better approximations to the 
%   zeroes of a real-valued function.
%
% Input:
%   f - input funtion
%   df - derived input function
%   x0 - inicial aproximation
%   tol - tolerance
%   nmax - maximum number of iterations
%
% Output:
%   x - aproximation to root
%   ex - error estimate
%
% Example:
%       [ x, ex ] = newton( 'x^3-4', 0, 0.5*10^-5, 10 )
%


    if nargin == 3
        tol = 1e-4;
        nmax = 1e1;
    elseif nargin == 4
        nmax = 1e1;
    elseif nargin ~= 5
        error('newton: invalid input parameters');
    end
    
    f = inline(f);
    df = inline(df);
    x(1) = x0 - (f(x0)/df(x0));
    ex(1) = abs(x(1)-x0);
    k = 2;
    while (ex(k-1) >= tol) && (k <= nmax)
        x(k) = x(k-1) - (f(x(k-1))/df(x(k-1)));
        ex(k) = abs(x(k)-x(k-1));
        k = k+1;
    end

end

Related Solutions

Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user...
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin.
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT