Simultaneous equations

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Simultaneous equations

benhartley903
Dear all,
I am relatively new to R and have had some difficulty in understanding the user manual for a package that I have downloaded to evaluate non-linear simultaneous equations.
The package is called systemfit.
Does anyone have any experience of this package?
What I am trying to do is solve a non linear simultaneous equations...

Could anyone give me an example (please) of the code that would be needed to return solutions for the following system using systemfit (or any other better package):

y=1/x
y=sin(x)

within a range of say x=-10 to 10 (x in radians)

Thanks, I really appreciate your help in advance.

Ben
Reply | Threaded
Open this post in threaded view
|

Re: Simultaneous equations

Berend Hasselman
benhartley903 wrote
Dear all,
I am relatively new to R and have had some difficulty in understanding the user manual for a package that I have downloaded to evaluate non-linear simultaneous equations.
The package is called systemfit.
Does anyone have any experience of this package?
What I am trying to do is solve a non linear simultaneous equations...

Could anyone give me an example (please) of the code that would be needed to return solutions for the following system using systemfit (or any other better package):

y=1/x
y=sin(x)

within a range of say x=-10 to 10 (x in radians)

Thanks, I really appreciate your help in advance.

Ben
systemfit is intended for estimating a system of simultaneous equations.
You seem to want to solve a system.
I assume you want to solve this system for x and y.

You could use package nleqslv as follows:

library(nleqslv)

fun <- function(x) {
     f <- numeric(length(x))
     f[1] <-  x[2] - 1/x[1]
     f[2] <-  x[2] - sin(x[1])
     f
}
x.start <- c(1,1)
nleqslv(x.start,fun)  # will give 1.1141571 0.8975395

x.start <- c(2,2)
nleqslv(x.start,fun)  # will give 2.7726047 0.3606717


Berend
Reply | Threaded
Open this post in threaded view
|

Re: Simultaneous equations

Peter Dalgaard-2
In reply to this post by benhartley903
On 09/02/2010 09:25 PM, benhartley903 wrote:

>
> Dear all,
> I am relatively new to R and have had some difficulty in understanding the
> user manual for a package that I have downloaded to evaluate non-linear
> simultaneous equations.
> The package is called systemfit.
> Does anyone have any experience of this package?
> What I am trying to do is solve a non linear simultaneous equations...
>
> Could anyone give me an example (please) of the code that would be needed to
> return solutions for the following system using systemfit (or any other
> better package):
>
> y=1/x
> y=sin(x)
>
> within a range of say x=-10 to 10 (x in radians)
>
> Thanks, I really appreciate your help in advance.
>
> Ben

Systemfit is not even in the same ballpark...!

I'd just rewrite the above as

x * sin(x) - 1 = 0

make a graph to bracket the roots and then use uniroot.

> f <- function(x) x*sin(x)-1
> curve(f, interval=c(-10.10)
> f(c(0,2,5,7,10))
[1] -1.0000000  0.8185949 -5.7946214  3.5989062 -6.4402111

So the roots are

> uniroot(f,interval=c(7,10))$root
[1] 9.317241
> uniroot(f,interval=c(5,7))$root
[1] 6.43914
> uniroot(f,interval=c(2,5))$root
[1] 2.772631
> uniroot(f,interval=c(0,2))$root
[1] 1.114161

and 4 more symmetrically below zero.

--
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Simultaneous equations

benhartley903
In reply to this post by benhartley903
Thanks Peter
Actually I should have specified. These are not actually the functions I ultimately want to solve can't be rearranged explicitly and substituted. I do need a way to solve simultaneously.

Ben
Reply | Threaded
Open this post in threaded view
|

Re: Simultaneous equations

Cren
In reply to this post by Berend Hasselman
Hi all,

I was wondering why I get errors trying to solve this:

simeq <- function(x) {
  f <- numeric(length(x))
  f[1] <- x[1] * dnorm((log(x[1]/D) + (r + x[2]^2/2) * T)/(x[2] * sqrt(T))) - D * exp(-r * T) * dnorm((log(x[1]/D) + (r + x[2]^2/2) * T)/(x[2] * sqrt(T)) - x[2] * sqrt(T))
  f[2] <- dnorm((log(x[1]/D) + (r + x[2]^2/2) * T)/(x[2] * sqrt(T))) * x[2] * x[1]
}
x.start <- c(1000000,0.2)
nleqslv(x.start, simeq)


or, alternatively, this

simeq <- function(V_0, sigma_v) {
  f <- numeric(2)
  f[1] <- V_0 * dnorm((log(V_0/D) + (r + sigma_v^2/2) * T)/(sigma_v * sqrt(T))) - D * exp(-r * T) * dnorm((log(V_0/D) + (r + sigma_v^2/2) * T)/(sigma_v * sqrt(T)) - sigma_v * sqrt(T))
  f[2] <- dnorm((log(V_0/D) + (r + sigma_v^2/2) * T)/(sigma_v * sqrt(T))) * sigma_v * V_0
}
x.start <- c(1000000,0.2)
nleqslv(x.start, simeq)


It says the length of function results is different  from that of x or that 'sigma_v' is missing... what does this mean? I thought I did not need to initialize sigma_v's value 'cause this is a function's variable...

Thank you