![Given a function y = f(x), X = (x0M, . . . ,xn)T with yi f(x) and xo < x1 < . . . < Xn, and one value a, write a Matlab function [y]=[][P1(X,n,a) to do the following: 1. Calculate the coefficients of the interpolation polynomial P(x) for the points (20,yo) (x %) using Newton divided-differences as in Algorithm 3.2. 2. For the given value a, calculate y = P(o) y is the output of your function. 3. Draw the graphs of y = f(x) and y Po(x) on the interval [20, aal together on one coordinate system.](http://img.homeworklib.com/questions/49084b20-caf1-11ea-8964-4dd0854870a4.png?x-oss-process=image/resize,w_560)
The first two parts should be solved by Matlab. This is from an intro to Numerical Analysis Class and I have provided the Alog 3.2 in below. Please write the whole codes for me.
Alog3.2
% NEWTONS INTERPOLATORY DIVIDED-DIFFERENCE FORMULA ALGORITHM
3.2
% To obtain the divided-difference coefficients of the
% interpolatory polynomial P on the (n+1) distinct numbers
x(0),
% x(1), ..., x(n) for the function f:
% INPUT: numbers x(0), x(1), ..., x(n); values f(x(0)),
f(x(1)),
% ..., f(x(n)) as the first column Q(0,0), Q(1,0), ...,
% Q(N,0) of Q, or may be computed
if function f is supplied.
% OUTPUT: the numbers Q(0,0), Q(1,1), ..., Q(N,N) where
% P(x) = Q(0,0) + Q(1,1)*(x-x(0)) + Q(2,2)*(x-x(0))*
% (x-x(1)) +... + Q(N,N)*(x-x(0))*(x-x(1))*...*(x-x(N-1)).
syms('OK', 'FLAG', 'N', 'I', 'X', 'Q', 'A', 'NAME', 'INP','OUP',
'J');
syms('s','x');
TRUE = 1;
FALSE = 0;
fprintf(1,'Newtons form of the interpolation polynomial \n');
OK = FALSE;
while OK == FALSE
fprintf(1,'Choice of input method:\n');
fprintf(1,'1. Input entry by entry from keyboard \n');
fprintf(1,'2. Input data from a text file \n');
fprintf(1,'3. Generate data using a function F \n');
fprintf(1,'Choose 1, 2, or 3 please \n');
FLAG = input(' ');
if FLAG == 1 | FLAG == 2 | FLAG == 3
OK = TRUE;
end
end
if FLAG == 1
OK = FALSE;
while OK == FALSE
fprintf(1,'Input n \n');
N = input(' ');
if N > 0
OK = TRUE;
X = zeros(N+1);
Q = zeros(N+1,N+1);
for I = 0:N
fprintf(1,'Input X(% d) and F (X(% d)) ', I, I);
fprintf(1,'on separate lines \n');
X(I+1) = input(' ');
Q(I+1,1) = input(' ');
end
else
fprintf(1,'Number must be a positive integer \n');
end
end
end
if FLAG == 2
fprintf(1,'Has a text file been created with the data in two
columns?\n');
fprintf(1 'Enter
Y or N \n');
A = input(' ','s');
if A == 'Y' | A == 'y'
fprintf(1,'Input the file name in the form - ');
fprintf(1,'drive:\\ name.ext\n');
fprintf(1,'For example: A:\\ DATA.DTA\n');
NAME = input(' ','s');
INP = fopen(NAME,'rt');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input n \n');
N = input(' ');
if N > 0
X = zeros(N+1);
Q = zeros(N+1,N+1);
for I = 0:N
X(I+1) = fscanf(INP, '% f',1);
Q(I+1,1) = fscanf(INP, '% f',1);
end
fclose(INP);
OK = TRUE;
else
fprintf(1,'Number must be a positive integer \n')
end
end
else
fprintf(1,'Please create the input file in two column ');
fprintf(1,'form with the X values and \n');
fprintf(1,'F(X) values in the corresponding columns.\n');
fprintf(1,'The program will end so the input file can ');
fprintf(1,'be created.\n');
OK = FALSE;
end
end
if FLAG == 3
fprintf(1,'Input the function F(x) in terms of x \n');
fprintf(1,'For example: cos(x)\n');
s = input(' ');
F = inline(s,'x');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input n \n');
N = input(' ');
if N > 0
X = zeros(N+1);
Q = zeros(N+1,N+1);
for I = 0:N
fprintf(1,'Input X(% d)\n', I);
X(I+1) = input(' ');
Q(I+1,1) = F(X(I+1));
end
OK = TRUE;
else
fprintf(1,'Number must be a positive integer \n');
end
end
end
if OK == TRUE
fprintf(1,'Select output destination \n');
fprintf(1,'1. Screen \n');
fprintf(1,'2. Text file \n');
fprintf(1,'Enter 1 or 2\n');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive:\\
name.ext\n');
fprintf(1,'For example: A:\\ OUTPUT.DTA\n');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end
fprintf(OUP, 'NEWTONS INTERPOLATION POLYNOMIAL \n \n');
% STEP 1
for I = 1:N
for J = 1:I
Q(I+1,J+1) = (Q(I+1,J) - Q(I,J)) / (X(I+1) - X(I-J+1));
end
end
% STEP 2
fprintf(OUP, 'Input data follows:\n');
for I = 0:N
fprintf(OUP,'X(% d) = %12 .8f F(X(% d)) = %12 .8f
\n',I,X(I+1),I,Q(I+1,1));
end
fprintf(OUP, '\n The coefficients Q(0,0), ..., Q(N,N)
are:\n');
for I = 0:N
fprintf(OUP, '%12 .8f \n', Q(I+1,I+1));
end
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file % s created successfully \n',NAME);
end
end
Answer A
interpolation_polynomial(i,points)
coefficients = [1/denominator(i,points)]
for k = 0 to points.length-1
if k == i
next k
new_coefficients =
[0,0,...,0] // length k+2 if k < i, k+1 if k > i
if k < i
m = k
else
m = k-1
for j = m downto 0
new_coefficients[j+1] += coefficients[j]
new_coefficients[j] -= points[k]*coefficients[j]
coefficients =
new_coefficients
return coefficients
Answer B
% Clear all windows and variables
clc
% Input the values for mass, height, friction coefficient, and
gravity
m=18; % Mass (kilograms)
h=10; % Height (meters)
mu=0.55; % Friction coefficient (no units)
g=9.81; % Gravitational acceleration (m/s^2)
% Input a reasonable range for x
x=[-100:100];
% Calculate F given the range of x
F=@(x) (mu.*m.*g.*(h.^2 + x.^2).^(1/2))/(x+mu.*h);
% Plot F as a function of x
fplot(F,[0,100])
title('Force versus Distance')
xlabel('Distance (meters)')
ylabel('Force (newtons)')
grid on
% Find the x value that gives F=90
??????????????
Answer C
% This figure will be used to plot the structure of the graph
represented
% by the current A matrix.
figure
dt = 2*pi/N_robots;
t = dt:dt:2*pi;
x = cos(t); y = sin(t);
agents=[ 2
2.5;
0.5 2.0;
0.5 1.0;
2.0 0.5;
3.5 1.0;
3.5 2.0;];
agents = p0;
agents = [x' y'];
% plot(agents(:,1),agents(:,2),'s','MarkerSize', 20,
'MarkerFaceColor', [1 0 1])
grid on
%xlim([0 4])
%ylim([0 3])
hold on
index=1;
% The following prints the non-directed graph corresponding to the
A matrix
for i=1:N_robots
for j=index:N_robots
if A(i,j) == 1
arrowline(agents([i j],1),agents([i
j],2),'arrowsize',600);
%
plot(agents([i j],1),agents([i j],2),'--','LineWidth',2.5);
end
end
end
set(gca,'FontSize',fontsize2)
title('Structure of the Graph','interpreter', 'latex','FontSize', 18)
The first two parts should be solved by Matlab. This is from an intro to Numerical...
im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...
class: numerical analysis
I wish if it was written in block letter
Sorry I can't read cursive
= COS Problem 1: Recall that the Chebyshev nodes x4, x1,...,xy are determined on the interval (-1,1] as the zeros of Tn+1(x) = cos((n + 1) arccos(x)) and are given by 2j +10 Xj j = 0,1, ... 1 n+1 2 Consider now interpolating the function f(x) = 1/(1 + x2) on the interval (-5,5). We have seen in lecture that if equispaced...
numerical methods
2+17), j = 0,1...... Problem 1: Recall that the Chebyshev nodes x0, 71,..., are determined on the interval (-1,1) as the zeros of Tn+1(x) = cos((n +1) arccos(x)) and are given by 2j +17 X; = cos in +12 Consider now interpolating the function f(x) = 1/(1+22) on the interval (-5,5). We have seen in lecture that if equispaced nodes are used, the error grows unbound- edly as more points are used. The purpose of this problem is...
Need help with MATLAB, what code should be added to
print the graph of the trapezoid rule code below?
%%Matlab code for Question 5. syms X y intlx exp(x),0,2); %integration of x*exp(x) fprintf("htlntegration of x"2*exp(x) for [O 3] is %2.5f.\n,y) %Integration using Trapizoidal rule nn [100 1000]; for ii 1:2 a:0; b-3; %Integration limit ns-1:nn(i) integral Values-zeros(size(ns)); ourFunction-@(x) x.*2.*exp(x); for n-ns val(n)-trapizoidal (ourFunction,a,b,nn(i); end fprintf nlntegration of x 2*exp(x) using Trapizoidal rule for [O 3]\n') fprintf('1tTrapizoidal integration value for n-%d...
Hi, can someone offer input on how to address these 4 remain parts the zybook python questions? 4: Tests that get_num_items_in_cart() returns 6 (ShoppingCart) Your output ADD ITEM TO CART Enter the item name: Traceback (most recent call last): File "zyLabsUnitTestRunner.py", line 10, in <module> passed = test_passed(test_passed_output_file) File "/home/runner/local/submission/unit_test_student_code/zyLabsUnitTest.py", line 8, in test_passed cart.add_item(item1) File "/home/runner/local/submission/unit_test_student_code/main.py", line 30, in add_item item_name = str(input('Enter the item name:\n')) EOFError: EOF when reading a line 5: Test that get_cost_of_cart() returns 10 (ShoppingCart)...
matlab
matlab
For this problem you will test a few interpolation approaches for the application of generating interpolated data. We'll do this by interpolating data that is sampled from a known mathematical function. The same function can then be used to compute true values at the interpolated points to use in error Consider the following mathematical function (Runge's function): 1+25r2 Write a function mfile that uses this formula to generate a set of data use those points along approaches outlined...
From the following code, define a new structure and use it to define the upper and lower limits of the trapezoidal and simpsons functions. #include<stdio.h> #include "math.h" #include <stdlib.h> double F1(double x) { return 1000 * exp(7*x) * cos(0.3*M_PI*x); } double F2(double x) { return pow(x,3) - (0.23*x) + 30.67; } void calculateIntegeralBySimpson(int n) { FILE *fp = fopen("data_Simpson_method.txt", "w"); int i; /* Calculating for first half of the interval */ double lowerLimit = -5, upperLimit = 0, x[n+1],...
I am creating a MATLAB game for my school project. The goal of the game is to create a 'Treasure Hunt Game' that asks the user to input the number of players, the difficult (easy, medium, or hard), and asks the user(s) to pick spots on a matrix until the correct spot is chosen, therefore winning the game. If a player misses the spot, the command window doesn't show how far away the treasure is, but what direction it is...
this is the function in matlab function [yint] = Newtint(x, y, xx) n= length(x); if length(y) ~=n , error('x and y must be same length'); end b= zeros(n,n); b(:,1) = y(:); %{ b(1,2)=b(2,1)-b(1,1)/x(2)-x(1) %} for j= 2:n for i= 1:n-j+1 b(i, j) = (b (i+1, j-1)-b(i, j-1))/(x(i+j-1)-x(i)); end end xt = 1; yint = b (1,1); for j = 1:n-1 xt = xt* (xx-x(j)); yint = yint+b(1, j+1) *xt; end end this is the input: >> x...
I have to write a Matlab function called "interpoly(x,y) which calculates the corresponding interpolation polynomial for given support points (xj,fj) with x=[x1,x2,...,xn] and f=[f1,f2,...,fn] and in interval x[min xj, max xj] plotted. Any method can be used to calculate the interpolation polynomial but I think Lagrange would be the best. One rule is that I can not use "polyfit" command.I have to test the programm with values of fj of the functions f(x)=cos(x) and f(x)=1/(1+x^2) each in the interval [-6,6]....