VBA EXCEL Fitting Curve with freely chosen function - excel

As start situation, I have an xy-chart with some values on it whose progression resemble an exponential function. I need to write a code that draws a fitting curve on the chart, but I have to use a particular function which is not exponential (because I need to get some coefficients from it).
One of the functions i need to use is K(C-x)²/(1+x) whereby k and C are the parameters I need.(They are two and it makes it a lot more complicated) Obviously you can't find this kind of structure on the fitting curve tool in Excel. Is there any possibility to have a fitting curve to a chart where you can write yourself the structure of the function?
Sorry if I don't add any written code, but i just need a hint to start writing.
Thank you

I did something to similar to this a while ago. The approach I took was to use the solver (as gary's student suggests). I think it was fired from VBA but that's unimportant.
Basically you'd have two input cells per row of data with your variables K and C. Then you need to find the difference (errors) between the values the function produces with the values in the input cells compared to the actual values (I think using errors^2 gives quicker conversion). You then sum the differences in another cell. When running the solver, you ask it to minimise the sum of differences by changing K and C.
Does that makes sense...?

Related

Excel Interpolate with logarithmic prediction

Is there a function within Excel to Interpolate while taking into account a logarithmic prediction?
At the moment I am using linear interpolation but would like to find a better way to fill in the blanks if possible.
There's no logarithmic regression or interpolation in Excel, even in the Anlaysis ToolPak. You'll need much more advanced software for that, such as MatLab.
If you're stuck working in Excel... here's a possible mathematical solution:
Rather than working with the raw data x and y, instead try plotting x and a^y, where a is the base. (Or plotting log(x,a) against y.) If you have the correct base a (and there's no vertical offset), you will then have a linear relationship from which you can perform a linear interpolation as normal, then convert the interpolated values back to actual values by taking the log of them.
If you don't know what a is, then you can instead calculate a line of best fit for an arbitrary a, calculate the standard residuals, and then use Problem Solver to modify a until you get the lowest possible standard residuals, at which point you have the best estimate of a.
Similarly if there is a vertical offset b, you'll need to test some variables there that also result in a linear relationship. Plot x against a^(y-b)

Microsoft Excel. Piece-wise Least-Squares Fit with Solver. My excel sheet produces right answers sometimes, wrong answers other times

I am trying to do a non-linear regression with data I have for my research. Since it is nonlinear, I can't use Simplex LP. Instead I was doing GRG Nonlinear with upper and lower bounds on all parameters.
It is weird because Excel produces answers that are right sometimes, and other times it is wrong. I have to manually change the parameters to arbitrary numbers, run Solver again, and hope that it is right. Let me show you my excel sheet.
https://drive.google.com/a/case.edu/file/d/0Bw0aJV0lW2eTaHFRUFhobVZ4NWs/edit?usp=sharing
Basically, I am looking to two linear lines to fit the data I have. The raw data can be divided into two portions, both linear. The point which the two lines cross is the Critical Value.
The correct output with my raw data is where the Critical Value = 0.006707. The last time I ran it, which is on the excel sheet, you can see that Critical Value = 3.36E-06.
If it would help you to understand this better in context, I am measuring surface tension of various systems. The critical value is so called Critical Micelle Concentration in my field.
Thanks Guys.
If you are using Excel solver, I think it is falling in local optima. Restarting the solver after setting the different variables to random values may help getting out of local optima values. Before restarting the solver, record the values of the objective function and compare it with the new value found by the solver.
Alternatively, use a better solver outside Excel. There are plenty of open source solvers available on the Internet.

Direct Power Regression in Excel

I want to obtain the R^2 values for several pairs of X v/s Y data.
It can be easily done in Matlab.
But in excel, I believe one needs to create new columns with logarithmic values or something.
Is there a direct, neat, formulas-based, Matlab-esque way to do this in Excel?
Matalb is the next generation of Excel ;) So definitely Excel is dull compared to Matalb. But dont' get demotivated, coz it's still a matrix based (Row,Col) arena...
Here is a function to try out:
RSQ function.
RSQ(known_y's,known_x's)
References for different ways:
calculate R-square in Excel
Edit:
If you need the logarithmic then you may have to use the following:
=RSQ(y-range,LN(x-range))

How to get points that intersect the trendline?

Basically I did the Cavendish experiment, and I have a damped sinusoidal wave plotted on Excel. With Position (mm) against Time (s).
My problem is that I have added a tread line through the wave function, and wish to calculate the points of which the wave function intersects the tread line. From this I will then be able to calculate the time period.
At the moment I'm just having difficulty getting the intersects..
Thanks
Excel is probably not the best tool to do what you want. In general, you want to fit your data to a damped Sin() function, e.g., F(x) = (A - B x) Sin(C x) or F(x) = A exp(-B x) Sin(C x), for linear or exponential dampening. Fitting the curve to the data will give you the values of the constants A, B, and C that fit the data best, and you can then proceed to simply solve F(x) == f_tread(x) to get the values x of intersection.
Programms like Mathematica, Matlab, or the free python based Sage are ideal for this and you can do it literally with two lines (well maybe three if you need to import the data first :-) ). I highly encourage you to give them a try if possible.
If you want/have to use Excel, then you can use it in a similar way for the data fitting part to get the constants A, B, C, etc. However, the part of finding intersections is trickier and unless you want to find the intersections by hand, you probably have to use an add-in like Solver and VBA script.
Finally a third way (since you seem to already have the formula for the damped sin wave) is to just plug the F==tread equation into WolframAlpha like so.

How can I solve a system of linear equations in Excel?

I have a system of 22 linear equations (exactly 22 equations and 22 unknowns) which are dynamically generated in an Excel spreadsheet. Is there a way to have Excel solve the system and produce a value for each of the unknown variables?
Another SO question has a lot of good information about the algorithms used to solve such equations. Cramer's rule in particular looks promising, but I'm not sure how to implement it in Excel.
Any help is appreciated!
This page gives the following simple method:
Enter the coefficient matrix in an nxn
range.
Enter in the vector of
constants in a n-tall column. Select a
blank n-tall column. It is important
that you not have only one cell
selected.
Type,
=MMULT(MINVERSE(coefficients_matrix_range),
constants_vector_range) in the formula
text box and press
[CTRL][SHIFT][ENTER]. It is important
that you not simply press [ENTER].
This should work adequately, since you only have a small system (22x22). While, ideally, you wouldn't invert the matrix directly, it shouldn't matter in this case.
You definitely don't want Cramer's rule. Due to its fairly poor numerical properties, it's more of theoretical than practical interest.
The Excel LINEST function should do it - and will return better results than MMULT(MINVERSE(...)...) in many cases.
You might also find the LOGEST, GROWTH and TREND functions useful.
What about Microsoft's Solver Foundation? I seem to remember there's some example for a smaller set of simultaneous equations in the documentation that comes with the express version.
You should not ever consider Cramer's rule to solve a linear system! It is so inefficient it is impossible to carry out for even small systems.
Excel is not the right tool to solve a linear system. If I were you I'd write a simple python script that reads the excel file, solve the equation (with numpy) and save the result in a file that excel can read. (even better, avoid Excel altogether if you can).
If you have any problems with in the inverse matrix calc, e.g. over / under flow / truncation etc, you could always use the Solver - else get a copy of numerical recipes in C and translate into VBA.
that can be linear equation solver we can do with mathematical solver by using maths equation of root lies between two more number of exponent part of the solver but find the better way of linear equation solver.

Resources