สร้างชุดที่มีค่าสัมประสิทธิ์ที่นำมาจากรายการค่า
Aug 18 2020
ฉันต้องการสร้างฟังก์ชัน $u(x) = \sum_{j=0}^9 a_j \cos{j\pi x}$ ที่ไหน $a_j$s มาจากรายการตัวเลขสุ่ม ฉันลองทำดังต่อไปนี้
Coeffs = RandomReal[1, 10]
u[x] := Series[(Part[Coeffs, j + 1]) Cos[π j x], {j, 0, 9}]
u[x]
และนอกจากนี้ยังมี
Coeffs = RandomReal[1, 10]
sum = 0
For[j = 0, j < 10, sum += Part[Coeffs, j + 1] Cos[j π x]]
u[x] = sum
u[x]
แต่สิ่งเหล่านี้ดูเหมือนจะไม่ได้ผล อันแรกแสดงข้อผิดพลาด:"The expression 1+k cannot be used as a part specification"
ในขณะที่เครื่องที่สองโน้ตบุ๊กยังทำงานไม่เสร็จ
อะไรคือวิธีที่ถูกต้องในการทำเช่นนี้?
คำตอบ
2 LouisB Aug 18 2020 at 14:45
Table
เป็นฟังก์ชันที่ดีสำหรับแอปพลิเคชันนี้ วิธีหนึ่งในการใช้งานTable
คือDot
ฟังก์ชั่นผลิตภัณฑ์
coeffs = RandomReal[1, 10];
basis = Table[Cos[π j x], {j, 0, 9}];
sum = Dot[coeffs, basis]
หากคุณไม่ต้องการ varaibles coeffs
หรือbasis
อย่างอื่นคุณสามารถทำได้
sum = Total @ Table[RandomReal[1] Cos[π j x], {j, 0, 9}]
สองแนวทางนี้ค่อนข้างบ่อย
3 cvgmt Aug 18 2020 at 14:32
อัปเดต
ขอบคุณคำแนะนำ
RandomReal[1, 10].Cos[Range[0, 9]*Pi*x]
ต้นฉบับ
Coeffs = RandomReal[1, 10]
u[x_] = Coeffs.(Cos[#*Pi*x] & /@ Range[0, 9])
2 kglr Aug 18 2020 at 15:16
ClearAll[f1, f2]
f1[x_] := Inner[Times, RandomReal[1, 10], Cos[Range[0, 9] π x]]
f2[x_] := Dot[RandomReal[1, 10], Cos[π Range[0, 9] x]]
SeedRandom[1]
f1[x]
0.817389 + 0.11142 Cos[π x] + 0.789526 Cos[2 π x] + 0.187803 Cos[3 π x] + 0.241361 Cos[4 π x] + 0.0657388 Cos[5 π x] + 0.542247 Cos[6 π x] + 0.231155 Cos[7 π x] + 0.396006 Cos[8 π x] + 0.700474 Cos[9 π x]
SeedRandom[1]
f2[x]
ผลลัพธ์เดียวกัน