I am having following problem:
I?m constructing model for calculation of area of triangle.
I know sides a, b, and gamma angle.
I wish to calculate the area using heron?s formula:
S <- sqrt(s*(s-a)*(s-b)*(s-c))
where
s <- (a+b+c)/2
and c is calculated using law of cosines:
c <- sqrt(a^2 + b^2 -2*a*b*cos(gamma))
since i am calculating a regression model, i need derivation of this
expression for area S.
something like (D(expression.S,c("a","b")))
To write it all into a single expression, it is too complicated, so i would
like to use some kind of substitution. however, if i try:
s.e <- substitute(expression((a+b+c)/2), list(c
expression(sqrt(a^2+b^2-2*a*b*cos(gamma))))),
I get>s.e
expression((a + b + expression(sqrt(a^2 + b^2 - 2 * a * b * cos(gamma))))/2)
which is not what I wanted
Can someone point me to the right direction?
--
View this message in context:
http://r.789695.n4.nabble.com/Substituting-inside-expression-tp3324092p3324092.html
Sent from the R help mailing list archive at Nabble.com.
Hi,
If I follow you correctly, you could write a function:
foo <- function(a,b,gamma){
c <- sqrt(a^2 + b^2 -2*a*b*cos(gamma))
s <- (a+b+c)/2
A <- sqrt(s*(s-a)*(s-b)*(s-c))
return(A)
}
I hope I didn't make mistakes, but it can still help you, I guess.
Ivan
Le 2/25/2011 10:11, zbynek.janoska at gmail.com a ?crit
:> I am having following problem:
> I?m constructing model for calculation of area of triangle.
> I know sides a, b, and gamma angle.
> I wish to calculate the area using heron?s formula:
> S<- sqrt(s*(s-a)*(s-b)*(s-c))
> where
> s<- (a+b+c)/2
> and c is calculated using law of cosines:
> c<- sqrt(a^2 + b^2 -2*a*b*cos(gamma))
>
> since i am calculating a regression model, i need derivation of this
> expression for area S.
> something like (D(expression.S,c("a","b")))
>
> To write it all into a single expression, it is too complicated, so i would
> like to use some kind of substitution. however, if i try:
>
> s.e<- substitute(expression((a+b+c)/2), list(c >
expression(sqrt(a^2+b^2-2*a*b*cos(gamma))))),
> I get
>> s.e
> expression((a + b + expression(sqrt(a^2 + b^2 - 2 * a * b *
cos(gamma))))/2)
>
> which is not what I wanted
>
> Can someone point me to the right direction?
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. S?ugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
On Fri, Feb 25, 2011 at 4:11 AM, zbynek.janoska at gmail.com <zbynek.janoska at centrum.cz> wrote:> > I am having following problem: > I?m constructing model for calculation of area of triangle. > I know sides a, b, and gamma angle. > I wish to calculate the area using heron?s formula: > S <- sqrt(s*(s-a)*(s-b)*(s-c)) > where > s <- (a+b+c)/2 > and c is calculated using law of cosines: > c <- sqrt(a^2 + b^2 -2*a*b*cos(gamma)) > > since i am calculating a regression model, i need derivation of this > expression for area S. > something like (D(expression.S,c("a","b"))) > > To write it all into a single expression, it is too complicated, so i would > like to use some kind of substitution. however, if i try: > > s.e <- substitute(expression((a+b+c)/2), list(c > expression(sqrt(a^2+b^2-2*a*b*cos(gamma))))), > I get >>s.e > expression((a + b + expression(sqrt(a^2 + b^2 - 2 * a * b * cos(gamma))))/2) > > which is not what I wanted > > Can someone point me to the right direction?Try this:> e <- substitute((a+b+c)/2, list(c = quote(sqrt(a^2+b^2-2*a*b*cos(gamma))))) > D(e, "a")(1 + 0.5 * ((2 * a - 2 * b * cos(gamma)) * (a^2 + b^2 - 2 * a * b * cos(gamma))^-0.5))/2 Also> library(Ryacas) # http://ryacas.googlecode.com > > a <- Sym("a"); b <- Sym("b"); gamma <- Sym("gamma") > > c <- sqrt(a^2+b^2-2*a*b*cos(gamma)) > deriv((a+b+c)/2, a)expression(2 * ((2 * a - 2 * b * cos(gamma))/(2 * root(a^2 + b^2 - 2 * a * b * cos(gamma), 2)) + 1)/4) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Thanks, but it is not exactly what i wanted to achieve. I need to calculate a derivation of this complex formula and I don?t think this would enable me to do so. But luckily, I have manages to built a "by-pass", so this is not necessary anymore zbynek -- View this message in context: http://r.789695.n4.nabble.com/Substituting-inside-expression-tp3324092p3327694.html Sent from the R help mailing list archive at Nabble.com.
Thanks, Your advice solved the problem for one substitution, but I needed to substitute twice: first s = (a+b+c)/2 and than c = sqrt(a^2 + b^2 -2*a*b*cos(gamma)) and I hoped I can do it simultaneously Luckily, I managed to go round the problem and this operation is not necessary anymore:) Thanks anyway zbynek -- View this message in context: http://r.789695.n4.nabble.com/Substituting-inside-expression-tp3324092p3327698.html Sent from the R help mailing list archive at Nabble.com.
On Mon, Feb 28, 2011 at 5:03 AM, zbynek.janoska at gmail.com <zbynek.janoska at centrum.cz> wrote:> Thanks, > Your advice solved the problem for one substitution, but I needed to > substitute twice: first s = (a+b+c)/2 > and than c = sqrt(a^2 + b^2 -2*a*b*cos(gamma)) and I hoped I can do it > simultaneouslyThere is no limitation to the number of substitutions using Ryacas:> library(Ryacas) > a <- Sym("a"); b <- Sym("b"); gamma <- Sym("gamma") > c <- sqrt(a^2 + b^2 -2*a*b*cos(gamma)) > s <- (a+b+c)/2 > deriv(s, a)[1] "Starting Yacas!" expression(2 * ((2 * a - 2 * b * cos(gamma))/(2 * root(a^2 + b^2 - 2 * a * b * cos(gamma), 2)) + 1)/4) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com