Problem Three Write a MATLAB function that given a set of x values (r1) and the
ID: 2082613 • Letter: P
Question
Problem Three Write a MATLAB function that given a set of x values (r1) and the corresponding values of f(zl) for some other function. f, and a second set of points r2, do a piece-wise linear interpo- lation of the function from the ri points to the z2 points. For example, let x1 C0:0.5:7] y sin x1); x2 C-1:0.05:8) then the function should return a piece wise linear approximation of the sin function evaluated at the points z2. That is you should be able to run plot (x2,z) where z is computed by your function, and see an piece-wise linear approximation of a sin curve. Turn in your solution, it must work for any function and sets of points z1 and r2 and cannot be based on similar built-in MATLAB functions.Explanation / Answer
%piecewise.m
function y = piecewise( fx,x1,x2 )
y = interp1(x1,fx,x2);
return;
end
%CALLING SCRIPT
clc;
close all;
clear all;
x1 = 0:0.5:7;
x2 = -1:0.05:8;
fx1 = sin(x1);
z = piecewise(fx1,x1,x2);
plot(x2,z);
for i = 1:1:9;
N = 2^i
x1 = -1:1/N:1;
x2 = -1:1/(10*N):1;
fx2 = 1./(1+(25*(x1.^2)));
strtitle = sprintf('N = %d',N);
figure
z = piecewise(fx2,x1,x2);
plot(x2,z);
title(strtitle);
fx2 = 1./(1+(25*(x2.^2)));
for j = 1:1:length(x2)
diff(j) = abs(fx2(j)-z(j));
end
MAXDIFF(i) = max(diff);
end
MAXDIFF
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.