Differentiating a Sum in MATLAB

In summary, if you have a sum of xi's, differentiation with respect to xn will give you 12xn^3. To differentiate y with respect to xn, use the repmat function to create a sum of xi's and then differentiate with respect to xn.
  • #1
sara_87
763
0

Homework Statement



How do i differentiate a sum in MATLAB?

So for an example, if I have:
[tex]y = x^2 + \sum^{n}_{j=1}3x_{j}^4[/tex]

I want to differentiate it with respect to x.
It should return: [tex]2x + \sum^{n}_{j=1}12x_{j}^3[/tex]

Homework Equations





The Attempt at a Solution



If I want to differentiate y = x^3+2x, this is what i would do:
>> syms x
>> y = x^3+2*x
>> diff(y,x,1)

this gives:

ans =

3*x^2+2


which is correct but how would i do differentiation if i have a sum?

thank you.
 
Physics news on Phys.org
  • #2
First of all, your problem is laid out wrong. If you differentiate xi with respect to x, you ought to end up with 0, I think. If your sum consists of x's rather than xi's, then sum x = Nx, and you needn't worry about the sums anymore.

I don't think the Matlab symbolic toolbox is too advanced, but I suppose you could "hack" it, e.g.:
z = sum(repmat(x, 1, 10));

Hope this answers your question.
 
  • #3
sorry, it should be:

[tex]y_n = x_n^2 + \sum^{n}_{j=1}3x_{j}^4+x_n^2[/tex]

so the derivative is:

[tex]y' = 2x_n + \sum^{n}_{j=1}3x_{j}^4+2x_n[/tex]

What does z = sum(repmat(x, 1, 10)); do?

thank you
 
  • #4
repmat just makes multiple instances of its input, e.g.
repmat(x, 1, 2) = [x, x]

I'm still a bit unclear as to what you're doing.
If you're differentiating yn with respect to xn, the sum reduces to 12xn^3, i.e.
[tex]
y_n' = 2x_n + 12x_n^3 + 2x_n
[/tex]

Do you then sum over the different y'n or what? That way you'd get
[tex]
y' = \sum_{i=1}^n y_i'= 2x_n + \sum_{i=1}^n 12x_i^3 + 2x_n
[/tex]

EDIT: This would be the same as taking the divergence, if I'm not mistaken.
 
  • #5
I want to defferentiate:

[tex]y_n = x_n^2 + \sum^{n}_{j=1}(3x_{j}^4+x_n^2)[/tex]

with respect to xn

I know that this should give:

[tex]2x_n + \sum^{n}_{j=1}(3x_{j}^4+2x_n)[/tex]

but i want to try to get this result using matlab.
 

1. What is differentiation in MATLAB?

Differentiation in MATLAB refers to the process of finding the derivative of a mathematical function or expression. It is a mathematical operation that calculates the rate of change of a function with respect to its input variables.

2. How do you perform differentiation in MATLAB?

To perform differentiation in MATLAB, you can use the diff function. This function takes the function or expression as its input and returns the derivative as its output. You can also specify the variable with respect to which you want to differentiate.

3. Can MATLAB differentiate any type of function?

Yes, MATLAB can differentiate any type of function as long as it is defined mathematically and can be represented in the form of an equation or expression. However, it may not be able to handle very complex functions or expressions.

4. How accurate are the results of differentiation in MATLAB?

The accuracy of differentiation in MATLAB depends on the accuracy of the input function or expression and the settings used in the diff function. By default, MATLAB uses a finite difference method to calculate the derivative, which may not always be accurate. To improve accuracy, you can use a smaller step size or specify a different method using the optional arguments of the diff function.

5. Can I plot the results of differentiation in MATLAB?

Yes, you can plot the results of differentiation in MATLAB using the plot function. You can plot the original function and its derivative on the same graph to visualize the relationship between them. This can be useful in understanding the behavior of the function and its rate of change.

Similar threads

  • Calculus and Beyond Homework Help
Replies
5
Views
490
  • Calculus and Beyond Homework Help
Replies
2
Views
669
Replies
7
Views
465
  • Calculus and Beyond Homework Help
Replies
7
Views
132
  • Calculus and Beyond Homework Help
Replies
2
Views
323
  • Calculus and Beyond Homework Help
Replies
10
Views
409
  • Calculus and Beyond Homework Help
Replies
6
Views
793
  • Calculus and Beyond Homework Help
Replies
10
Views
931
  • Calculus and Beyond Homework Help
Replies
6
Views
129
  • Calculus and Beyond Homework Help
Replies
7
Views
641
Back
Top