How to define a special function with some points
5 views (last 30 days)
Show older comments
Mojtaba Mohareri
on 10 Oct 2020
Edited: Mojtaba Mohareri
on 10 Oct 2020
I want to define a function like this
f(0.2)=1.42007;
f(0.4)=1.88124;
f(0.5)=2.12815;
f(0.6)=2.38676;
f(0.7)=2.65797;
f(0.8)=3.94289;
f(1)=3.55975;
to use these values in a For Loop. How can I define function f?
Thanks in advance.
0 Comments
Accepted Answer
Ameer Hamza
on 10 Oct 2020
Edited: Ameer Hamza
on 10 Oct 2020
You can use interp1()
x = [0.2 0.4 0.5 0.6 0.7 0.8 1];
y = [1.42007 1.88124 2.12815 2.38676 2.65797 3.94289 3.55975];
f = @(xq) interp1(x, y, xq);
Then you can also evaluate in for in-between points
>> f(0.2)
ans =
1.4201
>> f(0.3)
ans =
1.6507
>> f(0.55)
ans =
2.2575
6 Comments
Ameer Hamza
on 10 Oct 2020
It happens when the input xq goes beyond the range of values in x. In your case, if xq is less than 0.2 or higher than 1.0, interp1 will give NaN. To avoid this, use extrapolation.
clear all;
clc;
x = [0.2 0.4 0.5 0.6 0.7 0.8 1];
y = [1.42007 1.88124 2.12815 2.38676 2.65797 3.94289 3.55975];
f = @(xq) interp1(x, y, xq, 'linear', 'extrap');
x = 0.6;
h = 0.4;
D(1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2)
for i=1:2
h = h/2;
D(i + 1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2);
for j=1:i
D(i + 1,j + 1) = (4^j*D(i + 1,j) - D(i,j))/(4^j - 1)
end
end
More Answers (0)
See Also
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!