Goal Obtain a piecewise lineal interpolation of tin-function f(x) = e^-x/a sin(x
ID: 3667999 • Letter: G
Question
Goal Obtain a piecewise lineal interpolation of tin-function f(x) = e^-x/a sin(x^2 + b) where a is is the first digit of your SUID and b is its last digit. Interpolation should take plan: on the interval [0, 7]. Try equally spaced nodes with distance h = 1, h = 0.5, ... until you find one that works reasonably well. Report the spacing h you found suit aide in a comment. To begin with, define a function f (function handle would work). Calculate the nodes, the values of f at nodes, and the slopes of interpolation segments (the vector slope below: you may refer to the function pwL). Then evaluate the interpolant. One approach is this: Finally, plot both f and the interpolant together for comparison, using: different colors.Explanation / Answer
See the code below:
------------------------
SUID=input("Enter SUID:"); %get SUID from standard input
disp(SUID);
a=str2num(num2str(SUID)(1)); %first digit of SUID
b=str2num(num2str(SUID)(length(num2str(SUID)))); %last digit of SUID
%function f to calculate x and y
function [x,y,slope]=f(h,a=0,b=0)
x = 0:h:7;
if isnumeric(a) & isnumeric(b)
y = exp(-1.*(x./a)).*sin(x.^2+b);
end
%calculation of slopes
slopes=zeros(1,length(x));
for i=1:lenth(x)
if i == lenth(x)
slope(i)=(y(i)-y(1))/(x(i)-x(1));
else
slope(i) = (y(i+1)-y(i))/(x(i+1)-x(i));
end
end
distvec=[1.0 0.5 0.1 0.05 0.01]; %vector containing values to be considered for distances among data points
%loop for interpolation
for h=1:length(distvec)
[x,y,slope]=f(h,a,b);
t=linspace(0,7,100);
pl=zeros(size(t));
for k=1:length(x)
for i=1:length(t)
if t(i) == x(k)
pl(i) = y(k)+slope(k)*(t(i)-x(k));
else
id=sum(x<t(i));
pl(i)=y(id)+slope(id)*(t(i)-x(id));
end
end
end
hold on;
plot(y,pl,'b*');
end
---------------------------------------
Note: This is a draft implementation of the problem. You will need to modify this for the calculation of the slopes. As you have not given any reference what function pwL is, as mentioned in the problem, I have used the formula y2-y1/x2-x1 to calculate slopes. You may change that portion accordingly. Moreover, you can also change the distance interval values as per your requirement. Hope this helps you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.