Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.2K Views
|
4 Replies
|
1 Total Like
View groups...
Share
Share this post:

Define parameters accurately in custom functions?

Posted 6 years ago

Hi all,

I am new in this forum and hope that some of you can help. I have two questions which are related to eachother. It's all about defining parameters accurately and I think that I just don't know how to do this correctly.

General Problem

Assuming the function

f[x_]:=a*x^2

where a is a real number. How can I tell Mathematica that, for example, a is an integer? I found a solution using "Assumptions" as in

Assuming ->  Element[a, Integers]

but this doesn't work always (for example when I want to solve a system of equations, I got an error). I am looking for an easy solution where I can introduce my variables at the beginning and then use them all the time (the same as how we do it on paper).

One important example that doesn't work

The next problem (and I think that this is the same as the previous one) is as follows: I have the same function as above

f[x_]:=a*x^2

Now, I want to use the manipulation function as in

Manipulate[Plot[f[x],{x,-5,5}],{a,1,50}]

When I enter the function itself in the Manipulate argument (instead of "f[x]"), it works. But the way I described it, it doesn't. Probably, I have to introduce the parameter a, but I just don't know how.

Is something like this possible?

To illustrate my problem, another example:

When I enter

a=10

and

b=20

the command

Plot[x^2,{x,a,b}]

works perfectly. Now, I want to enter something like

a,b=real

Thanks in advance for the answers.

POSTED BY: Christian Stotz
4 Replies

Hey, Christian, how are you?

You can do

f[a_Integer,x_Real]:=a*x^2

so, when you use manipulate, you will be able to run the following code

Manipulate[Plot[f[a,x],{x,-5,5}],{a,1,50,1}]

enter image description here

POSTED BY: Estevao Teixeira

Hello Estevao,

thank you so much for helping!

POSTED BY: Christian Stotz

Hello Estevao,

Thank you so much for your reply! It works perfectly, however, do you also have a solution where I can define the parameter globally? In a way, that I can define a as an integer and use this to define several functions? Or do I have to enter every time I use the parameter that this is an integer?

Thank you so much!

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