Help with nesting subfunctions?
1 次查看(过去 30 天)
显示 更早的评论
I have an assignment that requires me to use a nested subfunction to produce a plot, and am not sure how to incorporate it. The exact problem states:
Recall Euler’s formula from analytical calculus: ei·x = cos(x) + i · sin(x), where i is the imaginary unit. A slight rearrangement yields:
cos(x) = ei·x − i · sin(x)
Create a nested subfunction that accepts one input argument x (which may be
a vector) and returns the cos function defined as above. Call this function and plot its output from −π ≤ x ≤ π using π/100 increments. Label axes and title it ”Euler Output”.
So far, I have not even been able to do this without a nested subfunction. MATLAB tells me that, "Subscript indices must either be real positive integers or logicals," but the given equation clearly needs to make use of imaginary roots.
采纳的回答
Wayne King
2012-9-25
编辑:Wayne King
2012-9-25
Your problem is here:
cos(x)=exp(1i.*x)-1i*sin(x);
You should spend some time reading the MATLAB Getting Started material, but you just assign the output to a variable.
cosx = exp(1i*x)-1i*sin(x);
Of course you have another problem with figure1( ) unless you have written a function called figure1().
2 个评论
Wayne King
2012-9-25
cosx =@(x) exp(1i*x)-1i*sin(x);
that gives you a function handle.
Then you can do:
x = -pi:0.01:pi;
y = cosx(x);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!