Question
Matlab Code: Use the erf function built into matlab to write this function. For this problem consider the following tabulated values of the error function:
Matlab Code:
Use the erf function built into matlab to write this function.
For this problem consider the following tabulated values of the error function:
x | 0.0 | 0.5 | 1.0 | 1.5 | 2.0 | |
erf(x) | 0.0 | 0.5205 | 0.8427 | 0.9661 | 0.9953 |
Write a function mfile that accepts a single, scalar input for x and uses these tabulated values to calculate and output the following four results (in order and all scalars) with the following set up:
function [erf, Et, erf_spline, Et_spline] = Inter(x)
1. An interpolated value for erf(x) using a 2nd order Lagrange interpolating polynomial.
2. The true error associated with the first output.
3. An interpolated value for erf(x) generated using a spline with "not-a-knot" end conditions.
4. The true error associated with the third output.
Start for function:
function [erf_lagrange2, Et_lagrange2, erf_spline, Et_spline] = student_solution(x) x_values =[0:0.5:2]; y = [0,0.5205,0.8427,0.9661,0.9953]; % Sort for closest two values if x <= 0.5 x1 = x_values(1); x2 = x_values(2); x3 = x_values(3); y1 = 1; y2 = 2; y3 = 3; elseif x > 0.5 && x <= 1 if (x-0) > (1.5-x) x1 = x_values(2); x2 = x_values(3); x3 = x_values(4); y1 = 2; y2 = 3; y3 = 4; else x1 = x_values(1); x2 = x_values(2); x3 = x_values(3); y1 = 1; y2 = 2; y3 = 3; end elseif x > 1 && x <= 1.5 if (x-0.5) > (2-x) x1 = x_values(3); x2 = x_values(4); x3 = x_values(5); y1 = 3; y2 = 4; y3 = 5; else x1 = x_values(2); x2 = x_values(3); x3 = x_values(4); y1 = 2; y2 = 3; y3 = 4; end else x1 = x_values(3); x2 = x_values(4); x3 = x_values(5); y1 = 3; y2 = 4; y3 = 5; end lagrange = ((((x-x2)*(x-x3))/((x1-x2)*(x1-x3)))*(y(y1)))+((((x-x1)*(x-x3))/((x2-x1)*(x2-x3)))*(y(y2)))+((((x-x1)*(x-x2))/((x3-x1)*(x3-x2)))*(y(y3)));
erf_lagrange2 = lagrange; Et_lagrange2 = (erf(x))-lagrange; end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started