write a function that takes any equation as an input

조회 수: 16 (최근 30일)
Konrad Brine
Konrad Brine 2019년 8월 20일
댓글: Konrad Brine 2019년 8월 20일
I saw that similar questions were asked previously by they were way too specific to be helpful for me.
I need to write a function that has inputs of a series of x values and an equation. This function should create an interpolating polynomial for this function using those given x values. It should then output the coefficients of this polynomial
My question is how do I write a function with an input of a function which outputs a matrix?
Code so far is just the one line:
function P(x) = splinecalc(Xlist, f(x))
And that line doesn't work. It should be able to take in things like sin(x) or x^3+15x-4 or e^x, etc. and it should output a 4×n matrix giving the coefficients of the natural (free) cubic spline through the n+1 points . The columns of the output matrix should correspond to the n cubic polynomials, in order. The kth row of your output matrix should give the coefficients of .

채택된 답변

the cyclist
the cyclist 2019년 8월 20일
Are you familiar with anonymous functions? That would presumably be the best way to pass "any function" as an argument.
Then inside splinecalc you can do what manipulations you need to create the interpolating polynomial.
  댓글 수: 1
Konrad Brine
Konrad Brine 2019년 8월 20일
I was not familiar with anonymous functions, I am new to this. Following those guidelines I am able to continue. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephan
Stephan 2019년 8월 20일
You might to want to achieve this:
% Inputs
xvals = 0:0.01:10;
fun = @(x) x.^5 + sin(x);
% call function
res = splinecalc(xvals, fun)
% plot results
hold on
fplot(fun,[xvals(1) xvals(end)])
plot(xvals, polyval(res,xvals))
hold off
% calculate cubic polynomial
function polynom = splinecalc(xvals, fun)
yvals = fun(xvals);
polynom = polyfit(xvals,yvals,3);
end

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by