Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.5K Views
|
8 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Custom function is not working

Posted 2 years ago

I have defined a function with some variable. When I am giving the value of those variables it is not giving me the value. What is the reason ?enter image description here

Attachment

Attachments:
POSTED BY: Indranil Maiti
8 Replies
Posted 2 years ago

Thanks, everyone for your time and help!

POSTED BY: Indranil Maiti
Posted 2 years ago

To use the simple notation for Pattern, e.g. x_, you need the "x" part to be a symbol. Otherwise Mathematica will interpret that expression differently. For example, look at

Subscript[Y, 0, A] _ // FullForm

It gives Times[Blank[], Subscript[Y, 0, A]], which is not a Pattern expression at all. What your definition of f ended up being was a definition for a single, specific f-headed expression. That is, if you evaluate

f[Subscript[Y, 0, A] _, Subscript[Y, 0, B] _, Subscript[\[Eta], A] _, Subscript[\[Eta], B] _, \[Lambda]_  ]

you will get a new expression, but it will be the exact expression you provided with no argument replacements happening.

I find using Subscript for variables very tedious, verbose, and error-prone, so I'm going to show you one way to define your function, but with more typical argument patterns. I'm also going to avoid special named characters, because it just clutters things up when posting code to this forum.

f2[Y0A_, Y0B_, etaA_, etaB_, lambda_] := 
  (1 - ((1 - Y0A)/(1 + etaA*lambda)^2) - ((1 - Y0B)/(1 + etaB*lambda)^2) + 
  ((1 - Y0A)*(1 - Y0B)/(1 + etaA*lambda + etaB*lambda - etaA*etaB*lambda)^2))

Notice I used := instead of =. That's not absolutely necessary, but it avoids problems if you had previous definitions for the symbols appearing here.

Test:

f2[10^-5, 10^-5, 10, 10, 10^-2]

gives 1178376600121/48400000000

POSTED BY: Eric Rimbey
Posted 2 years ago

Thanks a lot! It helps me and also the way you showed is very easy to use.

POSTED BY: Indranil Maiti
POSTED BY: Gianluca Gorni
Posted 2 years ago

You define the function f to have 5 parameters

but when you use the function f you only give it 4 parameters

so Mathematica thinks that f with 4 parameters is a different unknown function.

And there is no _ following your λ in your function definition

POSTED BY: Bill Nelson
Posted 2 years ago

And there appears to be a space between the Subscript expressions and the _.

Please post code that can be copied, pasted, and evaluated, not an image. As someone on MSE said

When you want your car to be repaired, you bring it to a workshop, not just show a photo to the mechanics. When you want your question to be answered, you post your code, not just a photo of it.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

here I am giving the code.

f[Subscript[Y, 0, A] _, Subscript[Y, 0, B] _, Subscript[\[Eta], A] _, 
  Subscript[\[Eta], 
    B] _, \[Lambda]_  ] = (1 - ((1 - 
       Subscript[Y, 0, A])/(1 + 
        Subscript[\[Eta], A]*\[Lambda])^2) - ((1 - 
       Subscript[Y, 0, B])/(1 + 
        Subscript[\[Eta], B]*\[Lambda])^2) + ((1 - 
       Subscript[Y, 0, A])*(1 - 
        Subscript[Y, 0, B])/(1 + Subscript[\[Eta], A]*\[Lambda] + 
         Subscript[\[Eta], B]*\[Lambda] - 
         Subscript[\[Eta], A]*Subscript[\[Eta], B]*\[Lambda])^2))
Attachments:
POSTED BY: Indranil Maiti
Posted 2 years ago

I have corrected that part but the problem has not resolve yet. Here I am sending the code you can check from there.

Attachments:
POSTED BY: Indranil Maiti
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard