How to create a function in the editor, then evaluate the function and plot the function.
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello all,
I am typing this function into the editor, and my goal is to evaluate the function at some points, and plot the function. Here is what I am typing in the editor.
function anon1
f=@(x) x^3 + 2x^2 - x
a=-10;
b=10;
TOL=.001
So, to evaluate the function at x=3 what do I type in the command window? And how do I apply the form given to plot. The form is fplot(fun,limits).
I was under the impression that I could just type anon1(3) to evaluate, but an error is showing.
Thanks
댓글 수: 0
채택된 답변
Youssef Khmou
2013년 2월 13일
Hi, Truxton,
In your code : you did two different things, making a function in M-file but inside that function , you used also "function_handle", you have two ways :
1) You create a function with its own Input/Output :
function [y]=Truxton(x) % function output=function_name(input)
y=x.^3 + 2*x.^2 - x;
a=-10;
b=10;
TOL=.001
Now in the command prompt , you tape per example :
>>x=5;z=Truxton(x)
>>time=[0:pi/100:10*pi];
>>Z=Truxton(time);
>>plot(time,Truxton)
>>fplot('Truxton',[0 100])
2)Or you can use the function handle directly in the command prompt :
>>f=@(x) x.^3 + 2*x.^2 - x
>>z=f(5);
>>z2=f([0:0.01:10];
I hope that helps
추가 답변 (1개)
Azzi Abdelmalek
2013년 2월 13일
편집: Azzi Abdelmalek
2013년 2월 13일
f(3)
to plot
f=@(x)x^3 + 2*x^2 - x
a=-10;
b=10;
fplot(f,[a,b])
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!