Help with creating a function
显示 更早的评论
Please write a MATLAB function with the following: (1) output a third-order polynomial function with the coefficients as the input variables; (2) Convert this function to function handle.
This is what I have but it will not work.
function output=poly3rd(a,s,d,f)
%this function creates a 3rd order polynomial with a,b,c, and d
%being the variables
output= a.*x.^3 +s.*x.^2 +d.*x+f
polynomial= @poly3rd;
end
poly3= @(x) *x.^3 +2.*x.^2+3.*x +4
2 个评论
dpb
2019-3-1
function output=poly3rd(a,s,d,f)
%this function creates a 3rd order polynomial with a,b,c, and d
- Why do your variables not match the help text? Not your actual problem, but still...why not document what you write correctly?
- x isn't defined in the function
- Can't refer to the function you're defining inside the function itself even if were valid syntax and if both of those were ok, polynomial would still only be visible inside poly3rd
- Bad syntax for poly3--no coefficients in the expression
Walter Roberson
2019-3-1
It is possible to refer to a function you are defining inside the function itself. It is done all the time for recursive functions.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!