Question

wnloads/HW5 pdf Create a MATLAB code to calculate function f (z) = ei8r at any given point using Taylor series. Write a script file called myTaylor.m and a function file called myFact.m to do the following: (a) The function myFact.m should return the factorial of a number when sent the number. In the script file: i. Set the starting point as r0-1 and fo = f(d)-6.049647 ii. Ask the user to enter his/her desired point and store it in x1 iii. Calculate the true solution for f(r1) and store it in TrueF1 iv. Calculate the first term of Taylor series at x1 and store it in TaylorF1 v. Calculate the true relative error of TaylorF1 and store it in TrueError vi. Use while loop and calculate next terms of Taylor formulation until the true relative error is less than 2%. (b) Once the next term is calculated, it should be added to the current value of TaylorF1 and the TrueError should be updated. i. Print the values of TaylorF1 and TrueError after each calculation ii. Count the number of Taylor formulation terms and print them (c) Run your program twice i. For x1 1.13 (you tan compare the results of this case with Question 2 ii. For one other x1 value of your choice. -IMG 6938JPG AIMG 6937 PG 4 5 6 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Define a function in a file named average.m that accepts an input vector, calculates the average of the values, and returns a single result.

function y = average(x)
if ~isvector(x)
    error('Input must be a vector')
end
y = sum(x)/length(x); 
end

Call the function from the command line.

z = 1:99;
average(z)
ans =
    50

Function in a Script File

Try it in MATLAB

Define a script in a file named integrationScript.m that computes the value of the integrand at 2m/3 and computes the area under the curve from 0 to . Include a local function that defines the integrand, y sin(z).

Note: Including functions in scripts requires MATLAB® R2016b or later.

% Compute the value of the integrand at 2*pi/3.
x = 2*pi/3;
y = myIntegrand(x)

% Compute the area under the curve from 0 to pi.
xmin = 0;
xmax = pi;
f = @myIntegrand;
a = integral(f,xmin,xmax)

function y = myIntegrand(x)
y = sin(x).^3;
end
y =

    0.6495


a =

    1.3333

Function with Multiple Outputs

Define a function in a file named stat.m that returns the mean and standard deviation of an input vector.

function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end

Call the function from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)
ave =
   47.3400
stdev =
   29.4124

Multiple Functions in a Function File

Define two functions in a file named stat2.m, where the first function calls the second.

function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end

function m = avg(x,n)
m = sum(x)/n;
end

Function avg is a local function. Local functions are only available to other functions within the same file.

Call function stat2 from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat2(values)
ave =
   47.3400
stdev =
   29.4124
Add a comment
Know the answer?
Add Answer to:
wnloads/HW5 pdf Create a MATLAB code to calculate function f (z) = ei8r at any given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a matlab HW that I need the code for, if someone could help me figure this out it would b...

    This is a matlab HW that I need the code for, if someone could help me figure this out it would be appreciated. The value of t can be estimated from the following equation: in your script file, estimate the value of π for any number of terms. You must ask the user for the desired number of terms and calculate the absolute error/difference between your calculation and the built-in MATLAB value ofpi. Display your results with the following message...

  • Solve the following problem in MATLAB. Use format compact for all work to suppress extra lines....

    Solve the following problem in MATLAB. Use format compact for all work to suppress extra lines. Show all work and add comments as needed to explain your logic/steps. 1. The function f(x) = e* can be approximated by the following Taylor series: n=0 The first few terms of the Taylor series are: e 1 + x + + + + ...... 2! 3! 4! Keep in mind that the "!" symbol denotes factorial. For example, the factorial of 4 =...

  • Consider the function f(x) = x3 – 2x2 + x Write the MATLAB code in the...

    Consider the function f(x) = x3 – 2x2 + x Write the MATLAB code in the format of "script file" using "Regula Falsi Method" with the estimated relative error of 0.000001 and upload the script file. Direction: 1) Please submit"script file", NOT "function file". If you submit a function file, I will assign ZERO score. 2) You can find some matlab code for the Regula Falsi Method on a web or some books. You can use any code you can...

  • This is the given code: /** * This program uses a Taylor Series to compute a...

    This is the given code: /** * This program uses a Taylor Series to compute a value * of sine. * */ #include<stdlib.h> #include<stdio.h> #include<math.h> /** * A function to compute the factorial function, n!. */ long factorial(int n) { long result = 1, i; for(i=2; i<=n; i++) { result *= i; } return result; } int main(int argc, char **argv) { if(argc != 3) { fprintf(stderr, "Usage: %s x n ", argv[0]); exit(1); } double x = atof(argv[1]); int...

  • Instructions: Submit your script in a file named hwk08.m to the dropbox before 11:59 pm on the du...

    Instructions: Submit your script in a file named hwk08.m to the dropbox before 11:59 pm on the due date NOTE: This assignment is neither quick nor simple. You will be well served to start on it early, and to ask for help if you need it. Being a more substantial assignment than earlier hwk, it is worth- s. When you ask your calculator for the value of a function for a specified argument, (e.g., sin(22), cos(74), In(6.5)), it almost certainly...

  • Please show exactly how it is in MATLAB The cosine function can be evaluated by the...

    Please show exactly how it is in MATLAB The cosine function can be evaluated by the following infinite series: cos ? = 1 − ?^2/ 2! + ?^4/ 4! − ?^6 /6! + ⋯ Create an M-file to compute cos(1.2345 rad) for up to and including eight terms, which is up to the term x^14/14!. a) Your program should compute and display the values of cos ? as each term in the series is added, e.g. cos ? = 1...

  • Problem 1 MATLAB A Taylor series is a series expansion of a function f()about a given point a. For one-dimensional real...

    Problem 1 MATLAB A Taylor series is a series expansion of a function f()about a given point a. For one-dimensional real-valued functions, the general formula for a Taylor series is given as ia) (a) (z- a) (z- a)2 + £(a (r- a) + + -a + f(x)(a) (1) A special case of the Taylor series (known as the Maclaurin series) exists when a- 0. The Maclaurin series expansions for four commonly used functions in science and engineering are: sin(x) (-1)"...

  • The nth-order Taylor polynomial for a function f(x) using the h notation is given as: Pa...

    The nth-order Taylor polynomial for a function f(x) using the h notation is given as: Pa (x + h) = f(x) + f'(a)h + salt) 12 + () +...+ m (s) n." The remainder of the above nth-order Taylor polynomial is defined as: R( +h) = f(n+1)(C) +1 " hn+1, where c is in between x and c+h (n+1)! A student is using 4 terms in the Taylor series of f(x) = 1/x to approximate f(0.7) around x = 1....

  • 1. Give an example of a differentiable function f and a point xo in the domain...

    1. Give an example of a differentiable function f and a point xo in the domain of f such that f(xo) # Poo(xo), where Poo is the Taylor series of f centered at x = 1. (To be perfectly precise, f(x0) + P(xo) means that lim En(xo) = 0, where En(xo) is the usual error function evaluated at xo.) n- 00 extex 2. The function cosh(x) = = - is called 2 the hyperbolic cosine and has many applications in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT