PDA

View Full Version : Calculus graphing



Roggie-Bear
10-20-2008, 02:56 PM
Doen anyone know how to set up a graph with multiple variables in excel? I can do it on a graphing calculator, but can't get it figured out in excel.:banghead:

Dave
10-21-2008, 08:04 AM
It seems like you want to chart a F(x). Here's some code to do that. Just replace your F(x) equation in the right spot. See comments for instructions. Note: sheet1 must have an image control and an iteration value in A1. The wb must also be saved before calling the sub. HTH. Dave

Option Explicit
Sub ChartFunction()
'charts function on sheet 2
'creates image file of chart. Places image in Sheet1 image control
'Removes chart and series data from sheet2
'loads image file to sheet1 image control and kills chart image file
'***The XL wb MUST be saved BEFORE calling this sub
'***Sheets("Sheet1").Image1 (image control) MUST exist
'***An inputted value is required for "Iter" (eg. [sheet1!A1])
Dim ChartRange As Range, Xvalue As Range, Yvalue As Range, Increment As Double
Dim Xmin As Double, Xmax As Double, Iter As Integer, Cnt As Integer, Fname As String
Application.ScreenUpdating = False
Iter = [sheet1!A1] 'Number of chart points (adjust to suit)
Xmin = 1 'lowest "X" value (adjust to suit)
Xmax = 100 'highest "X" value (adjust to suit)
Increment = (Xmax - Xmin) / (Iter - 1) 'chrt pt increments
'make chart data with f(x)
For Cnt = 1 To Iter
Sheets("Sheet2").Cells(Cnt, 1) = Xmin '"X" value
' "Y" value generated by function for "X" value eg. 2 * Sin(3 * X) + 8
'ie. Sheets("Sheet2").Cells(Cnt, 2) = F(x) equation
'Sheets("Sheet2").Cells(Cnt, 2) = Exp(Xmin) * Sin(Xmin ^ 2) ' "Y" value
Sheets("Sheet2").Cells(Cnt, 2) = 2 * Sin(3 * Xmin) + 8 ' "Y" value
Xmin = Xmin + Increment
Next Cnt
'make chart
Set Xvalue = Sheets("Sheet2").Cells(1, 1)
Set Yvalue = Sheets("Sheet2").Cells(Iter, 2)
Set ChartRange = Sheets("Sheet2").Range(Xvalue, Yvalue)
Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=ChartRange, PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
With ActiveChart
'fancy up chart here
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
'unlink data from image....
'create image file, delete data, delete chart
Fname = ThisWorkbook.Path & "\" & "ChartF(x).gif"
ActiveChart.Export Filename:=Fname, FilterName:="GIF"
Sheets("Sheet2").Range(Xvalue, Yvalue).Delete
ActiveChart.ChartArea.Select
ActiveWindow.Visible = False
Selection.Delete
'load chart image to image control on sheet1
'kill chart file
Sheets("Sheet1").Select
Sheets("Sheet1").Image1.Picture = LoadPicture(Fname)
Kill Fname
Application.ScreenUpdating = True
End Sub