Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.6K Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Using Lists in Function

Posted 3 years ago

Hello Everyone! I have just started using Mathematica. I want to use Lists inside a function depending upon the value of a parameter. Eg:- If Parameter value = 1, I want to create a list and if parameter value = 2 then I want to create another list. Then whatever be the parameter value I want to access the list created above and compute some values. Can someone guide me how to go forward with the above problem? Thanks in advance!

POSTED BY: Sayantan Ghosh
7 Replies
Posted 3 years ago

POSTED BY: Eric Rimbey
Posted 3 years ago

Sayantan,

I agree with Eric in general. But going back to your original code you might get a better result if you replace Equal (==) with SameQ (===)

enter image description here

POSTED BY: Hans Milton
Posted 3 years ago

enter image description here

I want to calculate the summation of the product of the Meijer G function, a constant depending on the running variable of the summation, and a list depending on the formal parameter. Here mu is my formal parameter and depending on its value I want to create 3 different lists of size 31. The problem I am facing is that error is showing with the message that "Part specification A[[1]] is longer than the depth of the object.". I am not able to understand how to correct this error and access the list properly.

POSTED BY: Sayantan Ghosh
Posted 3 years ago

Please understand that I would really like to help you, but this might get frustrating for you, because I need you to explain what problem you're having. I know it seems obvious to you, but I have no idea what you're struggling with.

You assigned a list to B. You can now do whatever you want with B. You can get its length: Length[B]. You can drop the first element: Rest[B]. I don't understand why you think you can't access it.

It might help if I point out that you're doing a lot of pointless stuff in your code. You don't need all of this imperative style initialization and reassignment stuff. Setting z = 0 isn't necessary. B[[2]] extracts the second element of B, but you aren't doing anything with it, so that's a pointless thing to do. If I'm understanding the line with Sum in it, you're just trying to add up the elements of the list. You can do that with Total. Since your B is "hardcoded" to two possible values, then the sum of B's elements is "hardcoded" to two specific values.

On top of that, you're defining Sum2 by first defining a function and then passing the arguments of Sum2 to that function. Why not just define Sum2 directly? Something like this:

Sum2[x_, y_] := If[x == 3, 6 + x + y, 15 + x + y]

If that's not producing the results you want, then please explain what results you're actually trying to get. Then we can show you how to get there.

POSTED BY: Eric Rimbey

Welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

The rules explain how to format your code properly. Posting code Images doesn't help other members to copy your code. Please EDIT your post and make sure code blocks start on a new paragraph and look framed and colored like this.

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

You can also embed notebook or attach notebook.

enter image description here

POSTED BY: EDITORIAL BOARD
Posted 3 years ago

enter image description here

Here depending on value of u I have created two lists. But I am not able to access any of these lists in the next line or able to perform any operation on them.

POSTED BY: Sayantan Ghosh
Posted 3 years ago

That's a very vague description of what you want to do. I could show you thousands of examples of parameterized functions that work on lists, but I'd rather not just start stabbing in the dark. Could you please provide a more concrete/specific example of what you'd like to be able to do?

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