Problem #1 write a function called my-expl that will take x as an input argument
ID: 3349419 • Letter: P
Question
Problem #1 write a function called my-expl that will take x as an input argument and return y and E as an output variable. The function will approximate the first 10 terms of exp(x) using Taylor series where 10 nl And the error E is given as E ly-exp(x) Test your code before submitting your function as .m file via blackboard. To help you start, use the following code function fy, Ej-My_exp1(x) y=0; % use for loop for n 0:10 96 new term xxxx 9 y- tnew term end E- absly-exp(x)); end Problem #2 Write a function called my-exp2 that will take x as an input argument and return y which is an approximation of Taylor series. The approximation will stop when the absolute error is less than 5*eps E is given as Test your code before submitting your function as .m file via blackboard. To help you start, use the following code My_exp2fx) function fy,nj y:0; E-1; 96 use while loop while (E>5 "eps) 96 new term xxxx % y=y+newterm E- absly-expfx))h - end endExplanation / Answer
clc
clear all
close all
x=input('Enter X value: ');
[y,E]=fun1(x);
fprintf('The value y obtained for x=%d using Taylor series is: %f ',x,y);
fprintf('Error is: %f ',E);
function:
function [y,E]=fun(x)
n=10;y=0;
for i=1:n
y=y+(x^i/factorial(i));
end
E=abs(y-exp(x));
Output:
Enter X value: 2
The value y obtained for x=2 using Taylor series is: 6.388995
Error is: 1.000061
Enter X value: 5
The value y obtained for x=5 using Taylor series is: 145.380601
Error is: 3.032558
2)
clc
clear all
close all
x=input('Enter X value: ');
[y,n]=fun1(x);
fprintf('The value y obtained for x=%d using Taylor series is: %f ',x,y);
fprintf('Number of terms taken are: %d ',n)
function:
function [y,n]=fun1(x)
n=0;y=0;E=1;
while(E>5*eps)
temp=x^n/factorial(n);
if(isnan(temp))
break;
end
y=y+temp;
E=abs(y-exp(x));
n=n+1;
end
Output:
Enter X value: 2
The value y obtained for x=2 using Taylor series is: 7.389056
Number of terms taken are: 1024
Enter X value: 5
The value y obtained for x=5 using Taylor series is: 148.413159
Number of terms taken are: 442
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.