Solving a differential equation

2 Ansichten (letzte 30 Tage)
Simone am 28 Nov. 2022
Kommentiert: Bjorn Gustavsson am 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
  5 Kommentare
Simone am 28 Nov. 2022
I was thinking about setting up a line that goes like
So If I know the value of the angular coefficient by my dataset, I could get the punctual value of N

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 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);
  3 Kommentare
Bjorn Gustavsson
Bjorn Gustavsson am 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));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Davide Masiello
Davide Masiello am 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)
sol = 
Then, turn the symbolic solution into an anonymous function.
f = matlabFunction(sol)
f = function_handle with value:
And finally fit the function to the data
fitobj = fit(C',V',f,'StartPoint',1)
fitobj =
General model: fitobj(x) = N.*1.0./x.^2.*(x.^2-1.0).*5.0 Coefficients (with 95% confidence bounds): N = 3.015 (3.01, 3.02)
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 Kommentare
Davide Masiello
Davide Masiello am 28 Nov. 2022
Maybe your data was already arranged in columns, in which case you need to delete the apostrophe after C and V in the fit function.

Melden Sie sich an, um zu kommentieren.


Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by