Excel Equation of line not correct - excel

I'm hoping someone will be able to tell me why the equation that Excel generated is not giving the correct results as it is graphed correctly.
I have some X and Y points that I will list below. I plotted those points in Excel and then plotted the trend line, and had it show me the equation of the trendline. When I take the equation and then plug in the X values I get very different answers back.
X and Y Values
X Y
0 3
3 2
5 1.4
7 1
10 0.5
18 0.1
When I set the intercept to 3, the equation of the trendline is y = 0.0088x5 - 0.1457x4 + 0.8753x3 - 2.224x2 + 1.4798x + 3
Screenshot of Excel window with equation
Any help is greatly appreciated.

I suspect you didn't set up your graph correctly.
Select a single cell in your table
Insert/Scatter (and decide which you want with regard to markers, etc)
Select the line and add Trendline
Set you parameters for the trendline
If you want to get the formula for the trendline from the "show formula" option, be sure to format the trendline label to be numeric with 15 decimals. Otherwise the equation will certainly not work, even if it appears to be correct.
Note that you can obtain the formula directly using the LINEST worksheet function.
=LINEST(Y,X^{1,2,3,4,5}) returns the array:
{0.0000399230399230442,-0.00152188552188569,0.0192991822991846,-0.0840134680134806,-0.217128427128402,2.99999999999999}
The last value in the array is the y-intercept
The slight differences are due to the use of different algorithms for the two methods.

Related

MATLAB fails to display plot, but Microsoft Excel can

My goal is to recreate a plot in Excel in MATLAB. In excel, I have coded:
1/(1 + (SINH($B$4^(1/2)*(1 - C6)^(1/2)))^2/(4*C6*(1-C6)))
In the above, cell B4 has the value 10. C6 through C500 take values starting at 0 upto 5 in succession (so C6 would be 0, and C500 would be C6+0.01+0.01+0.01... = 5), with increments of 0.01.
In other words, the column 'C' runs from 0 to 5 in increments of 0.01 (which is the x axis of the plot). I then apply the above formula to column C to obtain the graph below.
Excel displays the following, which is correct:
However, attempting to recreate the same graph in MATLAB results in an empty plot (which I show after the code)
v = 10
x = [0:0.01:5]
plot(x, 1/(1 + (sinh(v^(1/2)*(1 - x).^(1/2))).^2/(4*x.*(1-x))), 'LineWidth', 2)
grid on
xlabel('x')
ylabel('Transmittance')
I have tried restarting my computer, restarting MATLAB to no avail.
Thanks for your guidance!
You forgot two .'s:
1./(1 + (sinh(v^(1/2)*(1 - x).^(1/2))).^2./(4*x.*(1-x)))
^ ^
I advise you to first evaluate the expression and store in a varable, then you can immediately see that without this dot you will a single value. Plotting a vector and a single value doesn't really produce anything usable, but also does not throw an error, so it's tricky to debug without having the variable in the workspace.

Excel gives weird R square calculations?

This is really weird. I calculate R^2 values with Excel in two different ways and the results differ hugely. Why?
1) First I use Excel to do a linear regression via a graph, and use the "Add Trendline..." right mouse button functionality to specify Intercept = 0. The R square value shows -3.253. The regressed equation is Y = -0.1321 * X
2) Then I use Excel to do a linear regression via LINEST function. I highlight 5x2 rows and in the top left cell, I type "=LINEST ([Y vector]; [X vector], FALSE, TRUE). The False means the intercept is 0, and the True means Excel should print additional regression statistical information. Then I press CTRL + SHIFT + Enter. This will show me additional statistics, such as R^2 value in the third left cell. Which turns out to be 0.11166. The regressed equation is Y = -0.1321 * X
My question is; what am I doing wrong in calculating R^2 with the graph? Python and statsmodels.api confirms that R^2 is 0.11166, and the regressed equation is Y = -0.1321 * X.
Y =
0.0291970802919708
0.141801551718973
0.145668034655723
0.0691229530946433
0.0431577486597426
0.133618351873374
X =
-0.35551988
-0.20577599
0.10780785
-0.25028796
-0.42762184
0.02442197
Your calculation is correct. Scatter plot does not return correct R^2 when the intercept is 0. This is an formula fo R^2
where
If you use standard regression model, you use average value of y as y̅. But when you assume that the intercept equals 0, you need to set y̅ as zero. If you use the average value of y instead of zero, you get the R^2 = -3.252767.
You can see the calculation here. The SStot wrong column uses average value of y as y̅. Then the R^2 value equals to -3.252767. If you use 0 (as I did in SStot right column), then you get 0.111.
It is an old bug described by Microsoft here:https://support.microsoft.com/en-us/help/829249/you-will-receive-an-incorrect-r-squared-value-in-the-chart-tool-in-excel-2003
You need to use the LINEST function to get correct R^2 value.
Me and my fellow engineers just got tangled up in this. Based on this discussion and what we observed, the R^2 is wrong all of the time except when Excel calculates the best y-intercept. Any other y-intercept (either forced through Zero OR user-defined), is wrong.

charting trendlines together

I need to display this kind of data in one chart:
Which means:
Function A: Y= X + 10 ;
Function B: Y= X * 2 ;
but what I got from excel is:
Actually what I want is (sorry for the drawing):
My real data is like:
If I format it like this:
I can still not get the correct charts.
Updated to account for change of question
Your data structure is wrong to achieve this in Excel, see for example:
On the left you want your x axis values, don't put a header in this column otherwise it will plotted on your graph as a series (extra line).
You can fill in gaps in your data with #N/A and excel will ignore these points, joining the line to the next available point.

Excel scatter plot

Hello and good day to all
I have question related to excel graph. I have some set of values like this
Now I want to plot these values as a scatter graph. I want to to draw in such a way that by keeping X values same and 4 different Y plots i.e. A, B, C, D in a single graph. Meaning I dont want to merge these Y values on a single X value. Is there any way to do that? I thank you for your time and help.
The data does not make much sense. All X values are exactly the same, so all data markers will be at exactly the same X position.
To create a scatter chart with multiple series:
- select the data from A1 to B11
- insert a scatter chart
- select the B, C, D ranges (C1 to E11) and copy
- select the chart and use Paste Special > insert as new series with series name in first row
Since all X values are the same, many data points overlap and are not distinguishable.
Edit after comment: if you do NOT want to plot the values at their true X position (i.e. the value 1.4), then use four pairs of X / Y coordinates. Use X values 1 to 4 with A to D. Then use text boxes to replace the X axis labels or hide the labels and show the legend instead.

Importing XY coordinates from Excel to MatLab

I have imported my XY data (in the same excel worksheet) to matlab with all the X and Y coordinates in separate columns ('FHC'), X is in column b and Y in c. I want to plot these using using k-means.
I got stuck cause I don't like to plot all the coordinates, I like to include the first 10, skip 2 cells and then include the next 10 ect.I tried to specify cells like this (b2:b12,1);(c2:c12,2), it exceeds matrix dimensions?
How can I tell matlab from what cell to what other cell that I want it to include, possibly tell it to skip some cells in between? My code looks like this at the moment (but now it plots all the X's and all the Y's);
X =['FHC',(:,1);(:,2)] %written differently
opts = statset('Display','final');
[idx,ctrs,sumd,D] = kmeans(X,1,...
plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
hold on
plot(ctrs(:,1),ctrs(:,2),'ko',...
legend('Cluster 1','Centroid',...
If X and Y are cells then try this:
%if X is a n by 2 cell array
hold on
for ii=1:2:length(X)
plot(X{ii,1}(1:10),X{ii,2}(1:10),'r.','MarkerSize',12);
end
hold off

Resources