# Solving a differential equation

4 views (last 30 days)

Show older comments

Simone
on 28 Nov 2022

Commented: Bjorn Gustavsson
on 28 Nov 2022

Hello everybody, I can't seem to find a way to solve the following differential equation.

I have an array for V (which is my indipendent variable) and C (which is the dependent one), I need to find a punctual value of N for each value of C and V.

Looking online I've tried to use this line of code

ode = y == ((C).^3)/(k)*diff(V,C)

But I get the error : Error using diff Difference order N must be a positive integer scalar

### Accepted Answer

Bjorn Gustavsson
on 28 Nov 2022

According to your description you don't have a differential equation if you have the values of C and the known dependent values in V, and you want the values of N according to your equation. The best you can do from this is simply:

N = C^3/k*gradient(V,C);

HTH

##### 3 Comments

Bjorn Gustavsson
on 28 Nov 2022

@Simone, you have to keep track of when to use the element-wise operations ( ./ .* and .^ ) and when to use the matrix operations ( * / \ and ^). I also notice that I goofed on that one. The solution should be as @Davide Masiello gave above:

N = C.^3/k.*gradient(V,C);

Here we use the elementwise power in the first factor. Then, since k is a scalar constant it doesn't matter if we use / (right array divide) or ./. Then we take the gradient. In your variant you tried the right array divide with k*gradient(V,C) in the denominator which doesn't match the equation in your question. Your answer might therefore also be:

N = C.^3./(k.*gradient(V,C));

### More Answers (1)

Davide Masiello
on 28 Nov 2022

Since you have data for V and C you could fit them with the analytical solution of the differential equation.

Let's assume this is your dataset

k = 10;

C = [1 2 3 4 5 6 7 8 9 10];

V = [0.0158 11.3471 13.4291 14.1110 14.4800 14.5975 14.7361 14.8572 14.8940 14.9459];

Let us also assume that the initial condition is v(c=1) = 0.

You can find the analytical solution to the differential equation for a generic N.

syms x y(x) N

myode = diff(y,x) == N*10/x^3;

sol = dsolve(myode,y(1) == 0)

Then, turn the symbolic solution into an anonymous function.

f = matlabFunction(sol)

And finally fit the function to the data

fitobj = fit(C',V',f,'StartPoint',1)

The result is N = 3.015, which is good considering that I had produced the fake C and V data by using N = 3 and adding a bit of random noise.

##### 2 Comments

Davide Masiello
on 28 Nov 2022

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!