Question

(a) (4 points) Fill in the blanks in the following MATLAB function M file trap so that it implements the composilu trapezidul

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

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

clc%clears screen
clear all%clears history
close all%closes all files
format long
disp('part a & b (i)');
trap(0.1,3,20,1e-5)


function trap(a,b,maxiter,tol)
m=1;
x=linspace(a,b,m+1);
y=f(x);
approx=trapz(x,y);
disp('m integral approximation');
fprintf(' %5.0f %16.10f \n',m,approx);
for i=1:maxiter
m=2^i;
oldapprox=approx;
x=linspace(a,b,m+1);
y=f(x);
approx=trapz(x,y);
fprintf(' %5.0f %16.10f \n',m,approx);
if(abs((approx-oldapprox)/approx)<tol)
return;
end
  
end
fprintf('Did not converge in %g iterations\n',maxiter);
end


function y=f(x)
y=x.*cos(1./x);
end

- O X Pradeep EDITOR PUBLISH VIEW do E C . Search Documentation Insert fx Comment % 92% & Indent 1 - EDIT Run Section El Adva

Note: Brother According to Chegg's policy we are only allowed to answer first part if it is not a one word answer and there are many. So, I request you to post other part as separate posts

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
(a) (4 points) Fill in the blanks in the following MATLAB function M file trap so...
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
  • How can I plot this function in MATLAB?? trap( -5, 5, 50) function area = trap(x0,...

    How can I plot this function in MATLAB?? trap( -5, 5, 50) function area = trap(x0, xn, N) f=@(x) (6.*(exp(-x)).*(cos(x)).^2); h = ((xn-x0)/N); area=0; while (x0    area = area+(h/2)*(f(x0)+f(x0+h)); x0= x0+h;    end fprintf('area of trap', area); end

  • Write MATLAB code g(x)=x+a*f(x) f(x)=(e^x)+(x^2)-x-4 function f(x) is stored in fogp1.m (c) (1) Create a file...

    Write MATLAB code g(x)=x+a*f(x) f(x)=(e^x)+(x^2)-x-4 function f(x) is stored in fogp1.m (c) (1) Create a file gopgi.m to calculate the function glir, a) (see Preparation, ex. 7c) function [y] =gopg1(x,a) % input: x, a % output: y y= .....; (2) Create a file sucsub1.m and write a program that calculates the solution using the method of successive substitution. Calculate the values of g(x, a) by making multiple calls to the function gopg1(x, a). Take xo = 1.3 as starting value...

  • DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the...

    DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the following calculations. Your script will use the functions you create. Label all graphs appropriately. For this project, do not have your homemade functions fprintf anything, instead have all your fprintf commands within your script. Attach your published script file along with .m files for your functions. Exercise 1. A fundamental iterative method for finding the roots of equations of one-variable is known as Newton's...

  • This program has to be written in matlab 2. Write a function to find a root...

    This program has to be written in matlab 2. Write a function to find a root of f(c) using the secant method. Call this routine secant, call the file secant.m; its first line should be function [x,nf] = secant (fname, x0, x1, tol) fname is the name of the m-file which evaluates the function f(x), 20 and 21 are initial approximations to .c", and tol is a stopping tolerance. Your code should return an approximation x = 2X+1 to 3*...

  • Programming Language: MATLAB Problem 3: (5 Points) Write a function with the header: function [R] -myNewtonRaphson...

    Programming Language: MATLAB Problem 3: (5 Points) Write a function with the header: function [R] -myNewtonRaphson (f, fp, x0, tol) which takes as input f: a function handle fp: a function handle to the derivative of f (note how I do it in the test case). x0: the initial guess of the root tol: a tolerance above which the algorithm will keep iterating. Tips: . Be sure to include an iteration counter which will stop the while-loop if the number...

  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...

  • MATLAB Write an m-file capable of performing numerical integration for the equation using the simpsons and...

    MATLAB Write an m-file capable of performing numerical integration for the equation using the simpsons and trapezoidal functions: function [value, error] = simpsons(func, a, b, n, I) %retuns the value and error ret = 0; h = (b-a)/(n+1); %step size pts = a:h:b; % array of points for i=2:(n+3)/2 a = 2*i-3; b = 2*i-2; c = 2*i-1; ret = ret + (func(pts(a)) + 4*func(pts(b)) + func(pts(c))); end value = h*ret/3; error = abs(I - value)*100/I; %error between value and...

  • Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func:...

    Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func: the function/equation that you are required to integrate -a, b: the integration limits n: the number of points to be used for the integration I:Integral estimate a) Write a function capable of performing numerical integration of h(x) using the composite trapezoidal rule. Use your function to integration the equation with 9 points. Write a function capable of performing numerical integration of h(x) using the...

  • MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts...

    MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts in order to get full credit. Help me out, thank you f LAB SECTION NAME HW6P3 (30 points) following are infinite series representations of the function, un pra i a script file HW6P3 that determines the value of the function from the sum of the series for a given value of x. The program (1 pt) must prompt the user to enter a value...

  • I'm working on the newton's method on matlab, could someone help me and show what two...

    I'm working on the newton's method on matlab, could someone help me and show what two lines are needed to be added in the codes in order to make this function work? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x)...

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