calculate derivative of a function using handle

4 Ansichten (letzte 30 Tage)
denial nakayama
denial nakayama am 8 Jun. 2020
Kommentiert: Walter Roberson am 9 Jun. 2020
Hi, Matlab community
I have two scripts, one is
syms f(x)
f=@(x)func(x);
df=diff(f,x);
fplot(@(x) df(x),[0 10])
the other one is
function f=func(x)
if x >= 1 && x <= 2;
f=-x*1/4+2;
else
f=0;
end
And the error shows like:
Conversion to logical from sym is not possible.
How to solve ths problem?
Thank you for your attention
  2 Kommentare
KSSV
KSSV am 8 Jun. 2020
How did you use those functions...show us the full code.
denial nakayama
denial nakayama am 8 Jun. 2020
thank you for your quick answer.
I put them in the same folder and ran demo.m.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Ameer Hamza
Ameer Hamza am 8 Jun. 2020
You need to take the derivative of the original function and then apply the limits
syms x
f(x) = (-x*1/4+2);
df(x) = diff(f,x).*(1<=x&x<=2);
fplot(df,[0 10])
  7 Kommentare
Ameer Hamza
Ameer Hamza am 9 Jun. 2020
Not sure about the issue. I don't have R2014a. Maybe this will work
fplot(matlabFunction(df),[0 10])
Walter Roberson
Walter Roberson am 9 Jun. 2020
In R2014a, fplot() was not yet able to plot symbolic expressions.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 8 Jun. 2020
function f=func(x)
f = piecewise(x >= 1 && x <= 2, -x*1/4+2, 0);
  3 Kommentare
denial nakayama
denial nakayama am 9 Jun. 2020
Mine is 2014a and Thank you for your attention.
Walter Roberson
Walter Roberson am 9 Jun. 2020
function f=func(x)
syms t
temp(t) = feval(symengine, 'piecewise', '([t >= 1 and t <= 2, -t*1/4+2], [Otherwise, 0])');
f = temp(x);

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by