Closed Thread
Results 1 to 3 of 3

How to plot a formula with two variables

  1. #1
    Ali Baba
    Guest

    How to plot a formula with two variables

    Hi

    I would like to plot a formula in this form
    x + y = 0

    I know u may say "rearrange it to y = -x" and plot it. This is not what I
    want.

    I tried to write a code to generate data on the worksheet but I was
    unseccusseful

    this is what I am trying to do

    for x values,
    y = -10
    For x = 1 to 10
    Cells(i,1) = x
    Function = replace(function, "x", x)

    Do
    function = evaluate(replace(function, "y", y))
    y = y + 0.1
    loop until function = 0
    Cells(i,2) = y-0.1

    Next x

    Can anybody help to write this code??


  2. #2
    Bernard Liengme
    Guest

    Re: How to plot a formula with two variables

    Why you should want to do this I cannot guess!
    In column A (say A2:A22) enter the required x-values
    In B (B2:B22) enter any y-values --- a series of 1's will do
    In C2 enter =A2+B2 and copy down to C22
    Open Solver, clear the Target box; the By changing should read B2:B22; add
    constraint C2:C22=0; solve
    Plot A2:B22
    best wishes
    --
    Bernard V Liengme
    www.stfx.ca/people/bliengme
    remove caps from email

    "Ali Baba" <[email protected]> wrote in message
    news:[email protected]...
    > Hi
    >
    > I would like to plot a formula in this form
    > x + y = 0
    >
    > I know u may say "rearrange it to y = -x" and plot it. This is not what I
    > want.
    >
    > I tried to write a code to generate data on the worksheet but I was
    > unseccusseful
    >
    > this is what I am trying to do
    >
    > for x values,
    > y = -10
    > For x = 1 to 10
    > Cells(i,1) = x
    > Function = replace(function, "x", x)
    >
    > Do
    > function = evaluate(replace(function, "y", y))
    > y = y + 0.1
    > loop until function = 0
    > Cells(i,2) = y-0.1
    >
    > Next x
    >
    > Can anybody help to write this code??
    >




  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811
    Suspending judgement on why you want to do it this way:

    Sub mysub()
    Set rng=desired range
    With rng
    For x=1 to 10
    y=-10.1
    Do
    y=y+.1
    f=x+y
    loop until abs(f)<1e-6
    'I often prefer to test for almost 0 rather than exactly equal to 0 because of rounding error when decimal fractions are converted to binary in the IEEE system
    .cells(x,1).value=x
    .cells(x,2).value=y
    next x
    end with
    end sub

    check the syntax, I wrote that quickly. Debugged, it should do exactly what you've asked for, no less, and no more.

    I'm guessing this is just a test case for future, more complex problems. If this is true, you might consider looking into different numerical algorithms for solving f. A Newton-Raphson algorithm will be more certain of finding a solution, and will find it more effeciently than the algorithm we've used here. We also haven't dealt with cases (like x-y^2=0) where there is more than one possible y value for each x value. Bernard's approach using Solver in a worksheet will work well for most other equations, though it won't have a way of determining when multiple solutions exist.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1