How can I plot a sin (x^2) function

조회 수: 7 (최근 30일)
Drew Levis
Drew Levis 2019년 9월 16일
댓글: DGM 2022년 11월 11일
here is my code and it works but the figure is obviously not a sin(x^2) function from -pi to pi
syms x;
f=@(x)sin(x.^2);
x=[-pi pi];plot(x,f(x))
grid
  댓글 수: 2
Ayed
Ayed 2022년 11월 11일
Convert Fahrenheit degrees to Celsius degrees!
DGM
DGM 2022년 11월 11일
I have no idea why you decided to put this unrelated question here as an exclamation.
See the attached files.

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

채택된 답변

John D'Errico
John D'Errico 2019년 9월 17일
편집: John D'Errico 2019년 9월 17일
First, there is ABSOLUTELY NO reason to predefine x as a sym. So this line is completely irrelevant:
syms x;
When you do create x, you overwrite the symbolic version of x that you created before anyway.
Next, you created a function
f=@(x)sin(x.^2);
Good there. But then what?
What do you think this does?
x=[-pi pi];
It does NOT create the interval [-pi,pi]. Instead, it creates a vector of length 2, so TWO values, -pi and pi. Then when you plotted, using
plot(x,f(x))
it plots TWO points, connected with a straight line. And since the two poiints each had function value of zero, the line is perfectly horizontal. Compare that to this version:
f=@(x)sin(x.^2);
x=linspace(-pi,pi,100);
plot(x,f(x))
grid
See that I never needed to pre-define x as symbolic. (Why would you want to do that anyway? Just because you don't know the value of something, does not mean it must automatically be symbolic. This is perhaps the one of most common mistakes I see made by new users.)
Simpler yet, you might have done just this:
f = @(x) sin(x.^2);
fplot(f,[-pi,pi])
grid
  댓글 수: 1
Drew Levis
Drew Levis 2019년 9월 17일
I appericate the help. I started to think and come to a simlar answer when looking at the figure again. I am glad my thoughts were confirmed. THANK YOU again.

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 9월 17일
fplot(@(x)sin(x.^2),[-pi,pi])

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by