Question

8. (Matlab) Suppose we have a function hw5f.m that takes as input r and outputs the value for a function f(x). Write a Matl

(Matlab) Suppose we have a function “hw5f.m” that takes as input x and outputs the value for a function f(x). Write a Matlab program that inputs: • interval [a, b]; • m, the number of data points with evenly spaced nodes from x1 = a to xm = b, and values from f(x); • location z satisfying x2 < z < xm−1, where h = (b − a)/(m − 1); and outputs the value of the interpolaton polynomial using only the four data points with nodes closest to z. 2 (a) Write out or print out your program. You may find Matlab’s “floor” or “ceil” commands to be helpful. (b) Apply your program to the case m = 101 and xi evenly spaced, from −π to π, and yi = sin xi , and write out or print out your results when z = −3, −1, 0.5, 2.

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


z-2; [PP]-ppoly4 (f,interval,m,z); fprintfFor 2-82.2f the function value becomes P (x)-,z) disp (vpa(PP, 3)) for i-1length(x1,,,,움움움응움움움응움움움응움움움움움움움움움움움움움움各움움움움움움움움움움움움움움움움움움움움움움움움움움움움움움各各움움움 For z-3.00 the function value becomes P(x)-0.165*x*3 + 1.5

%%Matlab code for interpolating polynomial
clear all
close all

%function for which interpolation have to do
f=@(x) sin(x);
interval=[-pi,pi];
m=101;
z=-3;

[PP]=ppoly4(f,interval,m,z);
fprintf('For z=%2.2f the function value becomes P(x)=',z)
disp(vpa(PP,3))


%plotting the function
x1=linspace(-pi,pi,101);
y1=f(x1);
plot(x1,y1,'linewidth',2)

for i=1:length(x1)
    y2(i)=double(PP(x1(i)));
end

hold on
plot(x1,y2)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z=-1;

[PP]=ppoly4(f,interval,m,z);
fprintf('For z=%2.2f the function value becomes P(x)=',z)
disp(vpa(PP,3))

for i=1:length(x1)
    y2(i)=double(PP(x1(i)));
end

plot(x1,y2)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z=0.5;

[PP]=ppoly4(f,interval,m,z);
fprintf('For z=%2.2f the function value becomes P(x)=',z)
disp(vpa(PP,3))

for i=1:length(x1)
    y2(i)=double(PP(x1(i)));
end
plot(x1,y2)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z=2;

[PP]=ppoly4(f,interval,m,z);
fprintf('For z=%2.2f the function value becomes P(x)=',z)
disp(vpa(PP,3))


for i=1:length(x1)
    y2(i)=double(PP(x1(i)));
end

plot(x1,y2)
ylim([-2 2])
xlabel('X')
ylabel('Y(X)=sin(X)')
title('x vs. y(x) plot')
legend('Actual data','interp for z=-3',...
    'interp for z=-1','interp for z=0.5','interp for z=2')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function for interpolating polynomial
function [PP]=ppoly4(f,interval,m,z)
      
        %f is function for which polynomial have to find
        %interval is data interval [a,b]
        %m is number of data points
        %z is data point around which polynomial have to find
      
        a=interval(1); b=interval(2);
        h=(b-a)/(m-1);
        %all equidistant nodes
        x=a:h:b;
        %function values corresponding x
        y=f(x);
      
        %finding 4 data point close to z
        c=abs(x-z);
        [bb,i]=sort(c);
      
        %four data points around which interpolation have to do
        for j=1:4
            xx(j)=x(i(j));
            yy(j)=y(i(j));
        end
      
        %finding polynomial intepolation
        p=polyfit(xx,yy,3);
        syms x
      
        PP(x)= p(1)*x^3 + p(2)*x^2 + p(3)*x + p(4);
      
end
      
      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      
      
      
      
       

Add a comment
Know the answer?
Add Answer to:
(Matlab) Suppose we have a function “hw5f.m” that takes as input x and outputs the value for a function f(x). Write a Matlab program that inputs: • interval [a, b]; • m, the number of data points wit...
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
  • 1. (25 pts) Write a Matlab function with the header function [value- polynomialpiece(m,x.y,k,z) that inputs number of d...

    1. (25 pts) Write a Matlab function with the header function [value- polynomialpiece(m,x.y,k,z) that inputs number of data points m; vectors a and y, both with m components, holding r- and y-coordinates, respec- tively, of data points, and where the components of r are evenly spaced and in increasing order (rk/2,z,n+1-k/2) an even number k and a location distinct from the 0, > z . nodes; and outputs, using Lagrange form, the value at z of the deg S k-1...

  • 1. (25 pts) Given the following start for a Matlab function: function [answer] = NewtonForm(m,x,y,z) that inputs • number of data points m; • vectors x and y, both with m components, holding x- and y-...

    1. (25 pts) Given the following start for a Matlab function: function [answer] = NewtonForm(m,x,y,z) that inputs • number of data points m; • vectors x and y, both with m components, holding x- and y-coordinates, respectively, of data points; • location z; and uses divided difference tables and Newton form to output the value of the Lagrange polynomial, interpolating the data points, at z. 1. (25 pts) Given the following start for a Matlab function: function [answer] NewtonForm(m.x.yz) that...

  • 8. Write a MATLAB function that will take as inputs a set of data (x,y) and...

    8. Write a MATLAB function that will take as inputs a set of data (x,y) and output the best estimate of the integral using a Romberg Array. The function should perform two trapezoidal integrations using the full interval and half the interval, and then combine these integrations in a Romberg Array. Your function should also output the relative error of the integral. Please note that the data entered may not be evenly spaced and your function should make sure that...

  • P2) Write a MATLAB function myLinReg Username that is defined below. Use the in-built MATLAB func...

    P2) Write a MATLAB function myLinReg Username that is defined below. Use the in-built MATLAB function sum( ) to ease your programming (you are NOT allowed to use polyfit() and polyval()). Test the output of this function with the results obtained by hand for P1. Submit printout of program and a screenshot of the command window showing the results for P1. function [coeffvec, r2]-myLinReg_Username (xi,yi) %Function file: myLinReg-Username .m Purpose : 8 To obtain the parameters of a L.S. linear...

  • Write a Matlab function called sine_Taylor that inputs a positive integer n, a number x, and...

    Write a Matlab function called sine_Taylor that inputs a positive integer n, a number x, and outputs the nth Taylor polynomial of the sine function. Compute the absolute and error for n = 1: 10 at x = 1, obtained by comparing the output of your function to that of Matlab's built in sine function. Type on the Matlab console format long, then compute the previous error. Repeat the operation by typing format short. Comment on the result (quantify the...

  • #6 Write a Matlab program that finds numerically all the roots (or the zeros) of the...

    #6 Write a Matlab program that finds numerically all the roots (or the zeros) of the algebraic equation below in the interval 1.0 <=x<=3.0: sqrt(log(x^2+1))=x^2*sin(e^x)+2 Part a) Prompt the user to enter a positive integer number n, defined the range 2<=n<=15, and then verify if the number entered lies within the specifications. Your program must allow the user to reenter the number without the need to rerun the program. Part b) Create a user-defined function for the bisection method(see details...

  • 3. (a) Write a MatLab program that calculates for the function F(x, y) = ln(x + Va,2-y2) The prog...

    3. (a) Write a MatLab program that calculates for the function F(x, y) = ln(x + Va,2-y2) The program should use pretty) to display both the original function and the differentiated result, and also use fprintf() to print a label such as "F(x,y) -" and "dF/dxdy - " in front of both the function and the derivative. Then have your program also print out the derivative again after it uses simplify() on the result (b) Find the Taylor expansion of...

  • read instructions carefully please matlab only Write a MATLAB function for a BMI calculator that receives the inputs for body weight (lbs)and height (ft) from the user's program, calculates th...

    read instructions carefully please matlab only Write a MATLAB function for a BMI calculator that receives the inputs for body weight (lbs)and height (ft) from the user's program, calculates the BMI using the equation below, displays the BMI category, and outputs the BMI value tothe user's program W (kg) h2 (m2) BMI Display Normal if 18.5 s BMI 25 Display Overweight if 25 s BMI <30 Display Obese if BMI 2 30 Else display underweight So you will create two...

  • How do your write this in Matlab. Create a program that takes as input: An integer...

    How do your write this in Matlab. Create a program that takes as input: An integer nFunction that is either 1 or 2 indicating which of the two functions below to nsc * An integer n in [1.oc) indicating how many data points (ro."). ·The left endpoint a of an overall interval. (n泓) tote. The right endpoint b of an overall interval Your program should find the interpolating polynomial of degree less than or equal to of the requested function...

  • Homework 4 - False Position Find the zeros of the function f(x) within the interval (-4,...

    Homework 4 - False Position Find the zeros of the function f(x) within the interval (-4, 6) using the False Position method with f(-4) > 0 what you must know before you start working on the homework: f(-1) <0 f(x) = sin(21(x/5) + exp(x/5) f(+6) > 0 (a) Write a Matlab function that computes the values for f(x) when xis given as an input. (b) Write a Matlab script entitled "myplot.m” that plots the functions f(x) within the interval (-4,...

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