This is probably a dumb question but when I try to plot this function nothing appears:
format long
y = @(x) - sin(x);
z = @(x) x.^2;
test1 = @(x) y(x)-z(x);
x = fzero(test1,[0 1])
plot(test1)
It also gives my the following error:
error: unary operator '.'' not implemented for 'function handle' operands
error: called from
__plt__>__plt1__ at line 189 column 8
__plt__ at line 118 column 18
plot at line 229 column 10
ex1 at line 6 column 1

 채택된 답변

Alan Stevens
Alan Stevens 2022년 12월 6일

0 개 추천

Try
fplot(test1)

댓글 수: 4

Pedro Almeida
Pedro Almeida 2022년 12월 6일
Thank you so much!!!
Another possibility:
y = @(x) - sin(x);
z = @(x) x.^2;
test1 = @(x) -x.^2 - sin(x) %
test1 = function_handle with value:
@(x)-x.^2-sin(x)
x = fzero(test1, [0 1])
x = 0
t = linspace(-2,2);
f_val = feval(test1, t);
plot(t, f_val)
grid on
I'm sorry, but what does this line do exactly? I'm new to MATLAB so appologies if this is too simple.
f_val = feval(test1, t);
Askic V
Askic V 2022년 12월 6일
It evaluates the function test1. Please have a look at the documentation:
https://www.mathworks.com/help/matlab/ref/feval.html

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

추가 답변 (1개)

Sam Chak
Sam Chak 2022년 12월 6일

3 개 추천

You probably want to plot like this?
y = @(x) - sin(x);
z = @(x) x.^2;
fun = @(x) y(x) - z(x);
fplot(y, [-pi pi]), hold on
fplot(z, [-pi pi]),
xsol1 = fzero(fun, -1)
xsol1 = -0.8767
xsol2 = fzero(fun, 1)
xsol2 = 6.2882e-26
plot(xsol1, y(xsol1), 'o')
plot(xsol2, y(xsol2), 'p'), hold off
xlabel('x'), grid on,
legend('sin(x)', 'x^2')

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2022년 12월 6일

댓글:

2022년 12월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by