I have a equation i=a*exp(-c*F*E/(R*T))+b*exp(-d*F*E/(R*T)) *bold that I'm solving for a,b,c and d. The constants are F=96485, T=298, R=8.314. I'm trying to find a,b,c and d with given E and i value. How do I set it up in MATLAB? I've tried using fsolve but there was an error.

 Risposta accettata

Star Strider
Star Strider il 4 Giu 2018
See if this does what you want:
F=96485;
T=298;
R=8.314;
% % % i=a*exp(-c*F*E/(R*T))+b*exp(-d*F*E/(R*T)); % Original Equation
% % % a = p(1), b = p(2), c = p(3), d = p(4) % Define Parameters
fcn = @(p,E,i) p(1).*exp(-p(3)*F*E/(R*T)) + p(2).*exp(-p(4)*F*E/(R*T)) - i;
E = 42; % Use Correct Value
i = 24; % Use Correct Value
p0 = rand(4,1)*1E-6;
[Params,fval] = fsolve(@(p)fcn(p,E,i), p0)
If you have vectors of values for ‘E’ and ‘i’, use a nested loop to iterate through them:
E = [...];
i = [...];
for k1 = 1:numel(E)
for k2 = 1:numel(i);
Params{k1,k2} = fsolve(@(p)fcn(p,E(k1),i(k2)), p0);
end
end

6 Commenti

JuiChun Lin
JuiChun Lin il 5 Giu 2018
Could I just import let's say an excel file which has column E and i?
Star Strider
Star Strider il 5 Giu 2018
You could.
In that instance, use the second (nested for loops) version.
If the values for ‘E’ and ‘i’ always go together (same row of the matrix), one loop for both will do.
JuiChun Lin
JuiChun Lin il 5 Giu 2018
I think that code does the job, but if I were to not only interpret 4 points(results) like "rand(4)", how do I optimize the solution with more than 4 input?
I am lost.
Your parameter vector, ‘p’ has 4 elements, and ‘p0’ are the initial estimates for them, so that fsolve has something to begin with.
If you have another model with other than 4 (say ‘N’) parameters, ‘p0’ becomes:
p0 = rand(N,1)*1E-6;
or whatever the correct range of the initial parameters should be. Every problem is different.
JuiChun Lin
JuiChun Lin il 5 Giu 2018
why do you want to multiply the randomly generated numbers by E-6?
Star Strider
Star Strider il 5 Giu 2018
When I tested your function, it turned out that was the best magnitude range for them. Use whatever values you want.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by