How to find constants when 2 equations are equal?

Hi there! I have a function of (x) (and it's polynomial,degree=5) and I want to make it equal to "c1*exp(c2*x)". How can I find these two constants???? I would appreciate your helps...

 Accepted Answer

You can fit your polynomial with the core MATLAB funciton fminsearch and an extra line of code:
pf = @(x) x.^4 - 2*x.^3 + 3*x.^2 - 5*x + 4; % Polynomial Function
ef = @(c,x) c(1).*exp(c(2)*x); % Exponential Function
x = linspace(0, 5, 10); % Vector Of ‘x’ Values (Arbitrary)
SSECF = @(c) sum((pf(x) - ef(c,x)).^2); % Sum Squared Error Cost Function
c0 = [1; 1]; % Initial Parameter Estimates
[c, SSE] = fminsearch(SSECF, c0) % Estimate ‘c’, Return Sum-Squared-Error At Convergence
figure(1)
plot(x, pf(x), 'bp')
hold on
plot(x, ef(c,x), '-r')
hold off
grid
Substitute your own polynomial function. The plot is optional.
EDIT You need to include a y-offset term. With that, a single exponential fits very well:
d = load('Amin Moosavi a3.mat');
x = d.locs;
y = d.pks;
ef = @(c,x) c(1).*exp(c(2)*x) + c(3); % Exponential Function
SSECF = @(c) sum((y - ef(c,x)).^2); % Sum Squared Error Cost Function
c0 = [100; -1; 400]; % Initial Parameter Estimates
[c, SSE] = fminsearch(SSECF, c0) % Estimate ‘c’, Return Sum-Squared-Error At Convergence
figure(1)
plot(x, y, 'bp')
hold on
plot(x, ef(c,x), '-r', 'LineWidth',1)
hold off
grid
text(150, 470, sprintf('f(x) = %.1f\\cdote^{%.4f\\cdotx} + %.1f', c))

11 Comments

It worked but when I plotted it with my main data, the result was incorrect...
Dear @Star Strider I really appreciate your help. you solved my problem. thanks a lot . I don't know how should I say thank you. I'm from Iran. where are you from?
My pleasure.
You have thanked me in the best way possible here on MATLAB Answers by Accepting my Answer.
I’m from the western United States (about 106°W, 35°N). I have never been to Iran, but have several Iranian friends I met during my medical training and in engineering graduate school. I now count you among them.
It's my pleasure to be your friend. I would appreciate it. I would be happy if to be in contact with you
this is my Facebook page: facebook.com/smamnt
and this is my Telegram app ID: @smamnt.
and this is my insatgram : @moosavi.amin
And also my email : s.amin.moosavi.n@gmail.com
I hple to see you in Iran soon.
I do not do social media (I have no reason to use it).
I will be happy to help you if you have problems with your MATLAB code.
Dear @Star Stride I have the same problem again. but this time with some another data. Whne I use your code, this problem happens (I attached the photo). I can not understand your code to rewrite it by myself. could you help me in this?
You need to use different initial parameter estimates.
Change the ‘c0’ assignment to:
c0 = [0.2; -0.01; -1]; % Initial Parameter Estimates (a4)
and it will work. To make the text call compatible with other data sets, change it to:
text(mean(xlim), mean(ylim), sprintf('f(x) = %.2f\\cdote^{%.4f\\cdotx} %+.2f', c))
No other changes to my code will be necessary.
The first value of ‘c0’ should be close to the difference between the maximum value of your data and the approximate minimum of your data. The second value should converge with an initial estimate of -0.01. The third value should be the approximate minimum value of your data.
This is a common issue with nonlinear regression. It is occasionally necessary to experiment with different initial parameter estimates in order for the routine to converge on the correct final estimates.
Wow Wow Wow I don't know how should i thank you ... Thanks a lot my dear friend. I'm so happy you are in this world with me :)
As always, my pleasure!
Thinking more about ‘c0’, you can probably programme it to be:
c0 = [max(y)-min(y); -0.01; min(y)];
That should work for any of your vectors.
Dear @Star Strider
I did what you said and it worked. i don't know how should i say thank you. I hope God gives you every good thing you want.
thanks a lot
I hope to see you soon in my country.
As always, my pleasure!
I wish the same for you.
I doubt I’m going to do any traveling anytime soon, but thank you for the invitation.

Sign in to comment.

More Answers (1)

You cannot get out any meaningful results if you are working with a single x. In such a case the exact solution is
c2 = ln(a5*x^5+a4*x^4+a3*x^3+a2*x^2+a1*x+a0-c1)/x
so as you change c1, c2 changes as well, and for any given x you can force c2 to become imaginary (by using a large enough c1 that the polynomial minus c1 becomes negative, leading to ln() of a negative number.
For any two given x = x1, x2, you would have
c1 = a5*x2^5+a4*x2^4+a3*x2^3+a2*x2^2+a1*x2-exp(RootOf(exp(Z)-a5*x2^5-a4*x2^4-a3*x2^3-a2*x2^2-a1*x2+a5*x1^5+a4*x1^4+a3*x1^3+a2*x1^2+a1*x1-exp(Z*x1/x2)),Z)+a0
c2 = RootOf(exp(Z)-a5*x2^5-a4*x2^4-a3*x2^3-a2*x2^2-a1*x2+a5*x1^5+a4*x1^4+a3*x1^3+a2*x1^2+a1*x1-exp(Z*x1/x2),Z)/x2
here, RootOf(f(Z),Z) means the set of Z such that f(Z) becomes 0 -- the roots of the expression. You can see that the values you calculate would be very much tied up to the exact values of x1 and x2.
If you were hoping to take a series of values for the polynomial and fit the best c1, c2 for the entire series, then the result is going to depend a lot on which values you fit against.

Categories

Community Treasure Hunt

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

Start Hunting!