# Solving a differential equation

3 views (last 30 days)
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
Simone on 28 Nov 2022
Thank you for your answer, I get the mathematical aspect of the problem, what I don't understand is how to get the value of N in matlab. I don't understand if it's possible to get the answer
Simone on 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

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:
HTH
##### 3 CommentsShow 1 older commentHide 1 older comment
Davide Masiello on 28 Nov 2022
Edited: Davide Masiello on 28 Nov 2022
In your original response to @Chunru you said N is a constant, but now you say it must be a 351X1 column vector. Which one is it?
If it oughta be a vector, that's eaily obtained with @Bjorn Gustavsson's method, just write
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:
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:

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)
sol =
Then, turn the symbolic solution into an anonymous function.
f = matlabFunction(sol)
f = function_handle with value:
@(N,x)N.*1.0./x.^2.*(x.^2-1.0).*5.0
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.
Simone on 28 Nov 2022
Thank you for your answer, I've tried changing the assumption with a real value of my dataset but I get the following error
"Error using fit>iFit
X must be a matrix with one or two columns.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ..."
Davide Masiello on 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.