Question

In: Computer Science

Write a function in Matlab that will compute the approximation for ln(x) given the inputs to...

Write a function in Matlab that will compute the approximation for ln(x) given the inputs to the function are x and n. This function requires a running sum using a for-loop. ln (?) = lim?→∞∑ (−1)^(?−1)*(? − 1)^?/k

Solutions

Expert Solution


Hi,

Please find the Matlab script below:

--------------------------------------------------------
Matlab Function:

function [output] = lnOfX(x,n)
%function in Matlab that will compute the approximation for ln(x)
%given the
%Inputs to the function are : x and n
%For loop
sum=0;
for k=1:n
    %Accumulate
    sum = sum + (((-1)^(k-1))*(((x-1)^k)/k));
    % fprintf('Sum = %6.2f \n',sum);
end
output=sum;
end

Driver Script:

%Sample Driver Script
result = lnOfX(1/2,10);
fprintf('Approximation for ln(x) = %6.2f \n',result);

Screenshot:

------------------------------------------------------------------

Like the solution if you find it useful.

Hope this helps.


Related Solutions

Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to 3π/2 with increments equal to Δx,where Δx and n(the number ofterms used in the series approximation) should be input arguments of the function andshould be specified by the user.
Chepa’s utility function is given by U (x, y) = ln x + 4 ln y....
Chepa’s utility function is given by U (x, y) = ln x + 4 ln y. Assume that Chepa has endowments (10, 10) and that Py = 10 throughout the problem. (h) This part of the question is to investigate Chepa’s welfare under different prices. We will do it step by step. (i) By substituting out the M with the expression of Chepa’s endowment income (see part (g)), obtain Chepa’s gross demands as functions of Px. (ii) Plug your answer...
Write a MATLAB function named myaccel that accepts three inputs and has a single output. Inputs:...
Write a MATLAB function named myaccel that accepts three inputs and has a single output. Inputs: a list of time values a list of position values a single time value. Output: a single number which is obtained by numerically differentiating position with respect to time twice (forward difference method) and then interpolating the results based on the third input. Example: time=0:10; position=time.^3; myaccel(time,position,2.8) % should return 22.8
Write the demand functions for the following utility function: U = ln(x) + ln(y)
Write the demand functions for the following utility function: U = ln(x) + ln(y)
Write a Matlab m-script to compute the backward difference approximation A = f(a)−f(a−h) h of the...
Write a Matlab m-script to compute the backward difference approximation A = f(a)−f(a−h) h of the derivative T = f0(a) for f(x) = sin(x) and a = π/3 using each value of h in the sequence 2−n (n = 1,2,3,···,52).
Fourier Series Approximation Matlab HW1:     You are given a finite step function   x(t)=-1, 0<t<4...
Fourier Series Approximation Matlab HW1:     You are given a finite step function   x(t)=-1, 0<t<4          1, 4<t<8 .           Hand calculate the FS coefficients of x(t) by assuming half- range expansion,  for each case below and modify the code. Approximate x(t) by cosine series only (This is even-half range expansion). Modify the below code and plot the approximation showing its steps changing by included number of FS terms in the approximation. Approximate x(t) by sine series only (This...
Use matlab to plot Taylor approximation for f(x) for x near 0 Given f(x) = exp(x)...
Use matlab to plot Taylor approximation for f(x) for x near 0 Given f(x) = exp(x) Use subpots to plot and its absolute and realative erros for N= 1,2,3 PLease give matlab code for Taylor and explain in detail, Thank you
What is the minimum value of the function f(x)= (ln x) ^ln x for x >...
What is the minimum value of the function f(x)= (ln x) ^ln x for x > 1? A.) 1/e B.) 0 C.) e^-e^-1 D.) 1 E.) (ln 2) ^ln 2
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT