How to store output of Matlab command (like a pipe in UNIX)?

0

I am asking on this community per this question. Please let me know if this is incorrect and which community I should use.

I am trying to evaluate an integral in Matlab using:

int(x^(2)*sin(n*x),x)

Which will return:

(2*x*sin(n*x))/n^2 - cos(n*x)*(x^2/n - 2/n^3)

I then want to evaluate the result of this integral at specific values (similar to a definite integral, but I do not what to have to recompute the integral over and over again if I need to plug in lots of different points). Is there a way to store the result of the integral as a function? I have tried:

f = @(x) int(x^(2)*sin(n*x), x)

but then if I want to evaluate f at 0, f(0) tries to do: int((0)^(2)*sin(n*0), x), where I want it to do: (2*(0)*sin(n*(0)))/n^2 - cos(n*(0))*((0)^2/n - 2/n^3). Essentially, I want Matlab to take the integral and then evaluate at a certain value, without having to copy and paste the result of the int() command onto the next line? I would relate this to pipes in Linux, such that I would like to pipe the output of the int() call to the next line and evaluate its result at a specific value. Is there a way to do this?

wcarhart

Posted 2016-12-11T03:42:45.630

Reputation: 113

Answers

0

I solved my problem using the following:

f = symfun(int(x^(2)*sin(n*x),x),x);

Now I can use f(0) as I desire. I also found the following if I wanted to switch to numeric calculation:

fn = matlabFunction(f)

wcarhart

Posted 2016-12-11T03:42:45.630

Reputation: 113

0

You can do this by saving a symbolic version of the solved equation.

sums n x

f = sym('int(x^(2)*sin(n*x),x)')

f is now a symbolic representation of

(2*x*sin(n*x))/n^2 - cos(n*x)*(x^2/n - 2/n^3)

use the command pretty(f) to view it cleanly.

To use this equation to get a numeric answer you use the subs() command:

subs( f, [x n], [1,5])

I don't have matlab available to check my syntax but if this is the capability you are looking for it should be sufficient to get you started.

Argonauts

Posted 2016-12-11T03:42:45.630

Reputation: 4 000