アプリ版:「スタンプのみでお礼する」機能のリリースについて

プログラミングを勉強しているものです。
以下に示すプログラムを修正して原点を1とする半径が1の円の上半円の半円のグラフ描くプログラムを作りたいのです。直す箇所は2行だけと書いてあったのですが、何処をどのように直せば良いか教えていただきたいです。宜しくお願い致します。

Sub ex12()
fillfunc 0#, 10#, 60
mygraph 1, 1, 61, 2
End Sub
Sub fillfunc(x1 As Double, x2 As Double, nd As Integer)
Dim n As Integer
Dim x As Double, y As Double, dx As Double
dx = (x2 - x1) / nd
With Worksheets("Sheet1")
For n = 0 To nd
x = x1 + dx * n
y = Sin(x)
.Cells(n + 1, 1) = x
.Cells(n + 1, 2) = y
Next n
End With
End Sub
Sub mygraph(sr As Integer, sc As Integer, lr As Integer, lc As Integer)
ActiveSheet.ChartObjects.Add(200, 10, 240, 200).Select
ActiveChart.ChartWizard _
Source:=Range(Cells(sr, sc), Cells(lr, lc)), _
gallery:=xlLine, Format:=2, PlotBy:=xlColumns, _
categorylabels:=1, serieslabels:=0, HasLegend:=2, _
Title:="y", categorytitle:="x", valuetitle:="", extratitle:=""

End Sub

A 回答 (2件)

こうですかね。


Sub ex12()
fillfunc 0#, 2#, 60 '変更①
mygraph 1, 1, 61, 2
End Sub
Sub fillfunc(x1 As Double, x2 As Double, nd As Integer)
Dim n As Integer
Dim x As Double, y As Double, dx As Double
dx = (x2 - x1) / nd
With Worksheets("Sheet1")
For n = 0 To nd
x = x1 + dx * n
y = Sqr(1 - (x - 1) * (x - 1)) '変更②
.Cells(n + 1, 1) = x
.Cells(n + 1, 2) = y
Next n
End With
End Sub
Sub mygraph(sr As Integer, sc As Integer, lr As Integer, lc As Integer)
ActiveSheet.ChartObjects.Add(200, 10, 240, 200).Select
ActiveChart.ChartWizard _
Source:=Range(Cells(sr, sc), Cells(lr, lc)), _
gallery:=xlLine, Format:=2, PlotBy:=xlColumns, _
categorylabels:=1, serieslabels:=0, HasLegend:=2, _
Title:="y", categorytitle:="x", valuetitle:="", extratitle:=""

End Sub
    • good
    • 1

こんにちは



>プログラミングを勉強しているものです。
どこまでわかっていますか?

ご提示のプログラムは y=sin(x) をグラフ化するものです。
手順として、
 1)xの各値に対する、yの値を計算し表を作成する
 2)上記の表をエクセルのグラフ機能を利用してグラフ化する
というもので、それぞれをサブルーチン化してあります。

2)は指定されたセル範囲のデータを折れ線グラフに表示するだけのものなので、グラフの内容を変えたければ、表の値(=(x,y)のデータ群)変えればよいことになります。
値を計算している部分は何行もありませんので、そこだけ理解できれば、何を修正すれば良いかもわかると思います。
    • good
    • 1

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!