Question

In: Computer Science

Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single...

Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single file here in Canvas. To mYou may assume that f(a), f(a+h), f(a + 2h), and f(a) are all defined. However, you should NOT assume that f(a) is necessar

Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single file here in Canvas. To make grading easier, please do the following: • Close your function with "end". • Don't have your function print anything to the console; just return the output. To accomplish this: o Put a semicolon ";" at the end of each statement. o Put a percent "%" before each disp or fprintf statement. The function numdiff should take the following inputs, in order: • A real-valued function f. • A positive number h. • A real number a. The function numdiff should return as output a 3 x 9 matrix as follows: The first row should contain, in order, the following approximations for f'(a) using step size h: o The actual value of f'(a), computed symbolically. o The forward-difference formula. o The backward-difference formula. o The three-point midpoint formula o The three-point endpoint formula, assuming a is the left endpoint. o The three-point endpoint formula, assuming a is the right endpoint. . The five-point midpoint formula o The five-point endpoint formula, assuming a is the left endpoint. The five-point endpoint formula, assuming a is the right endpoint. • The second row should contain the absolute error in each approximation. • The third row should contain the relative error in each approximation.
You may assume that f(a), f(a+h), f(a + 2h), and f'(a) are all defined. However, you should NOT assume that f'(a) is necessarily nonzero; if f'(a) = 0, your function should still return each approximation and its absolute error. Use the MATLAB command "diff" to calculate the derivative of f(a). As many of you discovered in project 2, one way to calculate f'(a) is the following (assuming that the function handle f and the number a have already been defined): syms x df = diff( f(x) ) Df = eval( subs( df, x, a ) )

Solutions

Expert Solution

function dMat = numdiff(f,h,a)
syms x
df = diff(f(x));
trueValue = eval(subs(df,x,a));
dMat=zeros(3,9);
dMat(1,1)=trueValue;
dMat(1,2)=(f(h+a)-f(a))/h;
dMat(1,3)=(f(a)-f(a-h))/h;
dMat(1,4)=(f(h+a)-f(a-h))/(2*h);
dMat(1,5)=(-f(a+2*h)+4*f(a+h)-3*f(a))/(2*h);
dMat(1,6)=(f(a-2*h)-4*f(a-h)+3*f(a))/(2*h);
dMat(1,7)=(f(a-2*h)-8*f(a-h)+8*f(a+h)-f(a+2*h))/(12*h);
dMat(1,8)=(-25*f(a)+48*f(a+h)-36*f(a+2*h)+16*f(a+3*h)-3*f(a+4*h))/(12*h);
dMat(1,9)=(25*f(a)-48*f(a-h)+36*f(a-2*h)-16*f(a-3*h)+3*f(a-4*h))/(12*h);
dMat(2,:)=abs(trueValue-dMat(1,:));
if trueValue~=0%relative error will be calculated only if derivativeis not 0
    dMat(3,:)=dMat(2,:)/trueValue;
end


Related Solutions

Write a .m function file on MATLAB that reads the parameters stored in the file missile...
Write a .m function file on MATLAB that reads the parameters stored in the file missile data.txt into MATLAB. The function should have the following declaration: function [X0, Y0, Z0, m0, mf, Thmag0, theta, phi, Tburn] = read input( input filename, M id) where input filename is a string variable denoting the name of the file to be read and M_id is an integer which denotes the missile ID. The outputs are the initial position (X0, Y0, Z0), initial and...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
Write and submit at a Matlab function that sums up a specified number of odd-power terms...
Write and submit at a Matlab function that sums up a specified number of odd-power terms from the Maclaurin series of sin(x)  using following function format function s = etaylor(x, n) % Sum of first n non-vanishing terms of Maclaurin series of sin(x) % For example, etaylor(0.5, 1) should be 0.5, and etaylor(0.5, 2) should be 23/48   
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will...
Write a MATLAB function, called arbpoly, that computes a polynomial arbitrary nth degree. The function will take 2 inputs: 1) the first input will be a row vector, c, containing the coefficients of the polynomial, starting with the coefficient of the highest - degree term; 2) the second input will be a scalar, x, which is a real number at which the polynomial will be evaluated. The function's only output, y, will be the scalar value of the polynomial computed...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Using Matlab Write a function, called digits_function that is able to calculate the number of digits...
Using Matlab Write a function, called digits_function that is able to calculate the number of digits and the multiplication of the digits. The input of this function is N (entered number) and the outputs are number_digits and sum_digits.
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data...
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data arrays (call them x and f), and the array you would like to interpolate to (call it xstar). The function should have one output: the interpolated array ( call it fstar). The function should be able to interpolate x and f onto xstar using linear interpolation and give the result as fstar. The function may not use any intrinsic functions except length.
1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an...
1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an output, A1. The output A1 should be the area under a curve, f(x), for x starting at x_start and ending at x_end. The input r should be a vector (array) having x_start and x_end as its two elements. The input N should be an integer equal to the number of equallength sub-intervals in which the interval from x_start to x_end should be divided. Here,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT