#๐ Creating A Code
218 messages ยท Page 1 of 1 (latest)
@raven oyster
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
!paste
So, i know I start off my importing the libraries!
Let me do that real quick... is that fine so far or no?
I know I cannot help with that math, but I believe you meant scipy.integrate instead of scipy integrate
is this better?
And I actually calculated the math... not code it.. lemme show you what i did
I need to figure out how to code it though
By "calculate the following quantities" (Q3), is it expecting exact values?
I assume so...
Yes, looks correct, although I'm not sure of the exact structure/methods in scipy so cannot say for certain
i guess maybe we have all 256 values and we calculate this 256 times, since each number is different. ex. -12.8,-12.7,-12.6, etc.
and we need to calculate those quantities.. that is how i understand it
I guess so
it's a bit tough because if you look at my math, (if its correct), if i input value 1 into my equation, and then value 2 into my equation, the answers i receive are different... thats why i assume thats the case.
but rn I am trying to figure out how to input this into code.
This is one way to do it: ```py
for n in range(-128, 128):
x = n / 10
print(x)
shouldnt it be n/0.1?
sorry give me a minute to figure out why
oh i see.
i just did another example like 40*0.1=4 and 40/10=4
makes sense
mhm
so here is how my code is looking so far
i took a shot on part of part a
as well part of part b
what do you think so far?
first, how familiar are you with matplotlib?
tbh not that familar... my professor expects that we know coding prior to taking the class but i have little to no knowledge in python.. i watched a video on a course of python
prior to asking these questiosn
that's alright
!e
The simplest x,y plot looks like this: ```py
import matplotlib.pyplot as plt
xs = [1, 2, 3, 4, 5]
ys = [0, 2, 5, 4, 3]
plt.plot(xs, ys)
plt.savefig("fig.png") # replace with plt.show() in your code

:white_check_mark: Your 3.12 eval job has completed with return code 0.
Oh, i see! i remember seeing stuff like that in my videos.. only thing is, before I create the plot... how do i insert the value that i received in print to my equation of sinx(ax)
and then ig another question is making it use all 256 values..
and then i think i can plot!
i see!
i assume the x axis is going to be f(x)=sinx(Ax) and y axis is going to be the 256 values.. or vise versa?
the x and y values will be interpreted as coordinates
so in this example the coordinates (1,0) (2,2) (3,5) (4,4) (5,3) were plotted
oh i see
but how do i know the coordinate numbers... dont i need to solve them first... and i cant yk, put a list of every 256 values?
i need to code it
like i understand the list. but how do i make a list without knowing the values
well, you know the x coordinate of each point, right?
yes
then you can use the formula for f(x) to calculate the y coordinate of each point
please let me know if thats acceptable
not quite
can i make a function for this so i can call it into my list or no?
you can start with an empty list and add items to it
what do you mean?
You can add items to a list like this: ```py
x_values = [] # empty list
x_values.append(1)
x_values.append(2)
print(x_values) # [1, 2]
append?
so if you did that in a loop, you could store all the x values from -12.8 to 12.7
yes?
well, this is a for loop
some_list.append(thing) will add thing to the end of some_list
i guess where im kind of confused is ... lets say i make an empty list and then put append(1) and add onto it... do i have to manually do it from -12.7, all the way to 12.7 in one increment
you don't have to do it manually, that's what the for loop is for
but how do i call the loop into my append.. i need a function right, but how do i make my for loop a function (correct me if im using the wrong terminology)
you can call append inside the loop
can you provide an example.
!e ```py
lst = []
for num in range(5): # 0,1,2,3,4
lst.append(num)
print(lst)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, 1, 2, 3, 4]
see how lst starts out as empty but it gets populated by lst.append(num) inside the loop
oh i see! ty
why does it start empty. i always see it but i never understoof why
i see it in various codes but i never knew why
an empty list usually means stuff will be added later
oh okay!
like in this case, where we could list all the values from -12.8 to 12.7 inside the []
that makes a lot of sense
but instead we can add them all with a for loop
why not try run it?
yup
oh so something is wrong
for technical reasons, range can't accept decimal numbers
you have to use something similar to the for loop in lines 7-9
looks good
so ig i have to input the equation but how do i imput the value i received from the equation in the image into my new equation
Well yes.
if i input what i received for "a", i can receive a equation where if i input an x value, ill be able to calculate for F(X)
yup
how do i assign the function a to my equation a%b+1
I think defining a and b as 3 and 4 is a bit misleading here
okay!
ty!
lol im suprised i didnt think of that
i just made it harder for no reason
rn im trying to input the sinc equation
give me a minute
i tried this but sinc isnt found in the library... can i do math.sin(apix_axis)/apix_axis
its bc a = 2 so the equation would be sin(2pix)/2pix
math.sin does exist, but it will only take single x values
oh not math.sin
math.sinc
this is what i did... please let me know if im right so far... i think it should be correct.
you're getting closer
Can i please get a hint?
so now you can calculate f(x) for a given x value
now to do it for each x value in your range
??
not quite... x_axis is a list of all the x values
but you need to calculate this for each x value
you can use the same loop for both x and y
i.e.: ```py
x_axis = []
y_axis = []
for ...:
# calculate x value
x_axis.append(...)
# calculate y value
y_axis.append(...)
lol wait
im confuse
give me a few minutes
?
lol that didnt work
am i getting closer or no?
the calculation for the y coordinate has to happen inside the loop
because it will only work on single x values, not a list of them
?
closer
instead of making random changes, think logically about what the program will do
hint: what is line 19 doing?
well here is how i am approaching it... we have a for loop so that is going from -128 to 128 ... and we are stating in line 18 and 19 that we are dividing those number by 10 and adding a number to that list... and then i insert the equation
not really
it stores the y coordinates of each point in a list
how about something like this .. bc im making a list with my equation?
would you be able to help me work on this tomorrow? are you busy?
closer; you just need to change x_axis in the formula
to num/10?
yup, since that's the current x value
you could also assign it to a variable like so: ```py
for num in range(-128, 128):
x = num / 10
x_axis.append(x)
y_axis.append( ... ) # use 'x' instead of 'num/10'
my professor added coding tips, do you know what labels is... is it laveling the x axis and y axis title or the title or the grapg... im a bit confused
and Oh okay!
wait odd im getting an error
wdym?
sinc(0) = 1, right?
that has to be handled separately because the denominator will be 0
oh i see
you're right
if then statement... like if 0 , dont do anything, if not, then use the equation
?
I think it's worth defining a new function here
but yeah thats the gist of it
could you provide me an example on how you would do it in python.. ive done it in c++ but that was A WHILE AGO. I DONT REALLY RECALL IT AS MUCH
also are you busy tomorrow?
example of a function or if statement?
if
if condition:
...
else:
...
I have school tomorrow so I guess so
ios not ==
Are you looking for !=?
no like how do i say if not 0, is it if == 0, 'do not not use equation' and if =0 , 'isnsert equation'
If-else?
i see.. ill just search that up. what function should i make since you recommended it
sinc(x)
:0 uh i would recommend creating a new help page...
i completed up to part c ... if you can help me with part d and e, that would be great ๐
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.