Adam Bathor
Adam Bathor

Reputation: 1

Calculate value of symbolic equation in MATLAB

I have following MATLAB code:

>> syms x
>> z = 20.*exp(x)+cos(x)
>> derivative = diff(z)

How can I calculate value of derevative for any number, e.g. 6?

Following commands

>> clear all
>> x = 6
>> derevative = 20*exp(x)-sin(x)

works fine, but if x is symbolic I don't use x = 6 such as above example.

Upvotes: 0

Views: 5300

Answers (4)

simon3177
simon3177

Reputation: 1

you can use the function given below:

syms a b
subs(cos(a) + sin(b), [a, b], [sym('alpha'), 2])

it works perfect.

Upvotes: 0

Salvador Dali
Salvador Dali

Reputation: 222481

The right way to do this is the following:

syms x
z = 20.*exp(x)+cos(x)
derivative = diff(z)
subs(derivative, 6)

where the last line subs(derivative, 6) does the job, you need.

Upvotes: 3

rgb
rgb

Reputation: 1850

You can try this:

syms x;
z = 20.*exp(x)+cos(x);
derivative = diff(z);
x = 5;
result = eval(derivative)

Upvotes: 1

nes1983
nes1983

Reputation: 15756

There's a tutorial on how to do symbolic computation in MATLAB. From what I understand, it is a bit unorthodox to use MATLAB for symbolic computations, though.

Upvotes: 0

Related Questions