Solve using MATLAB! f(x) = arcsin(x) -sin^-1 (x) = Sigma _n = 0^N (2n)!/4^n(n!)^
ID: 3858245 • Letter: S
Question
Solve using MATLAB! f(x) = arcsin(x) -sin^-1 (x) = Sigma _n = 0^N (2n)!/4^n(n!)^2(2n + 1) x^(2n + 1), N = infinity and |x| lessthanorequalto 1 Write a MATLAB function, named my_f2, that will accept x and N as inputs and produce two outputs, corresponding to the approximated value of f(x) in degrees, and the percentage error between the exact value and the approximated value. The function should check the value of x and display an error if it's out of range: otherwise, it will report the outputs. Test your function for (x = 0.5 and N = 5), and for (x = 2 and N= 8). Test your function for (x = 0 and N = 5). Show how to correct the reported value of the error. For -1 lessthanorequalto x lessthanorequalto 1, and N = 8, generate a 2D plot, showing the approximate f(x), on the y-axis, versus x, on the x-axis.Explanation / Answer
my_f2.m
function [fx1 err1] = my_f2(x,N)
%handing x which should be |x|<1
if abs(x)<=1
fx1 = 0;
for n=0:N
fx1 = fx1+(factorial(2*n)/((4^n)*(factorial(n)^2)*((2*n)+1)))*(x^((2*n)+1));
end
%correcting reported value of error here asin(0) return 0 and something/0
%causes error to avoid that if x=0 I will make error as 0
if x~=0
err1 = ((asin(x)-fx1)/asin(x))*100;
else
err1 = 0;
end
else
fx1 = 'invalid';
err1 = 'input';
end
end
>> [fx1 err1] = my_f2(0.5,5)
fx1 =
0.5236
err1 =
5.0732e-004
>>
>> [fx1 err1] = my_f2(2,8)
fx1 =
invalid
err1 =
input
>>
>> [fx1 err1] = my_f2(0,5)
fx1 =
0
err1 =
0
>>
matlab code to plot fx versus x
x = -1:0.01:1;
N = 8;
for i=1:length(x)
[fx1 err1] = my_f2(x(i),N);
fx2(i) = fx1;
end
plot(x,fx2);