CS1112 Specific programming constructs

__EK486-0.zip 立即下载

CS1112 Fall 2024 Project 2

due Wednesday 9/25 at 11pm

Objectives

Completing this project will solidify your understanding of for-loops, while-loops, and nested loops. The computational theme of this project is simulation, with problem 1 using randomness as a computational tool. You also will play with graphics.

Ground Rule

Since this is an introductory computing course, and in order to give you practice on specific programming constructs, some Python functions/features are forbidden in assignments. Specifically, the use of lists and the break and continue commands is not allowed in this project. Only the built-in functions and language features that have been discussed in class and in the assigned reading may be used. For this project, do not define your own functions; we will do that later in Project 3.

1 Eclipse

The “Monte Carlo approximation of π” presented in Lecture 5 allows us to estimate π using randomness and proportion. Indeed, the method of Monte Carlo Approximation has many uses in science and engineering and one of them is to estimate the area of arbitrary shapes whose boundaries can be described by a set of equations.

The equation (x − h)2 + (y − k)2 − r2 = 0 describes a circle with ra-dius r centered at (h, k). Therefore, a pair of values x and y satisfy-ing

(x − h)2 + (y − k)2 <= r2

is a point that lies inside or on the circle.

Write a script. estimate area (i.e., write a file estimate area.py) to de-termine the area of the lune (crescent) or ring formed from overlapping a bigger circle with a smaller circle using Monte Carlo approximation. Your script. should produce a figure showing the “darts” in the three areas: the smaller circle, the bigger circle excluding the occlusion, and the rectan-gular “dart board” area not covered by the circles. Display as the title of the figure the estimated area. In the example output (figure) above, the bigger circle has a radius of 3 and is centered at (2, -1). The smaller circle has a radius 2 and is centered at (1, -1). You can choose any color combination that you like in order to display the “darts”.

Start by reviewing the notes and code from Lecture 5 on the Monte Carlo approximation of π! This problem is slightly different as you are estimating the area of a shape:

Note the new file mc pi.py posted with Lecture 7 that includes graphics in addition to the numerics of the dart throwing approximation of π. You will find the graphics commands demonstrated in mc pi.py useful.

Requirements and program development hints

❼ Import and use the modules random and matplotlib.pyplot. You may additionally need math.

❼ Each circle is represented by three values: radius, x coordinate of the cetner, and y coordinate of the center. Using meaningful variable names, you can “hardcode” the values, i.e., type into the script. the values, that we used to produce the example figure above to start program development. Those values give two circles that overlap. After you have a working program, try other values. Hardcoding the values instead of using input statements makes initial testing easier. When you submit your file you can leave these hardcoded values in or replace them with input statements if you prefer.

❼ Your script. should do the area estimation only if the two circles overlap. If the two circles do not overlap, your program should print a message to the console saying so and should neither estimate an area nor do graphics. Hint: One way to tell whether two circles overlap is to consider the distance between their centers.

❼ If the circles overlap, your code should set the dart board such that both circles lie entirely within the dart board and are not too small to be seen clearly on the dartboard.

❼ You need to choose the number of darts for the Monte Carlo approximation. In the early stages of program development, use a small number of darts in order to speed up experimentation and testing! Once you are satisfied with your program after testing, choose a number of darts that shows the circles reasonably well.

❼ The area approximation is to be shown as the title of the figure, not displayed in the console. (Review the camera fov problem of Project 1 for how to work with the figure title.)

2 Blinking Indicator Light

Blinking indicator lights are often used as a safety measure to warn people about a potential danger. They are common in places like crosswalks and construction sites. The blinking of these lights can be achieved by charging and discharging a capacitor. A capacitor can store electrons inside it and act as a battery for a short period of time. We can use the following equations to describe the voltage (v) as a function of time (t) of a capacitor as it is charging:

where v0 is the voltage of the battery, R is the resistance of a resistor, C is the capacitance of the capacitor, and tc is the charging time of the capacitor to reach its maximum voltage. Note that exp(x) denotes the natural exponent of x (i.e., exp(x) = e x ). After charging for tc time, the capacitor voltage reaches its maximum vmax, which is given by the following equation:

After the charging time, the capacitor immediately starts to discharge and it is described by the following equation:

The capacitor discharges until it reaches a minimum voltage given by vmin. The entire time taken for charging and discharging is given by:

Suppose that this capacitor powers an indicator light. We will use the following constant values in our programs: v0 = 10 volts, R = 10 × 103 ohms, vmin = 0.01 volts, and C = 10 × 10−6 farads. [Revised 9/18]

2.1 How long does it take to reach a certain voltage?

Write a script. time to target (i.e., write a file time to target.py) to determine and print to console how many seconds it takes the capacitor to reach a user-specified voltage given a user-specified charging time for max voltage. If the capacitor cannot reach the specified voltage, your script. should print a message saying so. Solicit the goal voltage in volts and the max charging time (tc) in seconds as user inputs. Both input values should be positive and your script. should re-prompt the user (as many times) as necessary to ensure that positive values are entered. You can assume that the user does enter numbers and not words. Hardcode the constant values given above.

Use a loop to solve the problem; do not solve for the time algebraically. If the user specified voltage is achievable, you should increment the time and compute the voltage of the capacitor until the goal voltage is reached. Given the constants above and the typical voltage and charging time of interest, a time step in the order of 0.001 second would be reasonable. (A smaller time step will give higher accuracy but at the expense of a longer computation time.) You may not need all the equations given above.

You will need to use the math module functions exp to calculate the natural exponential of a value and log to calculate the natural log of a value. For example, to calculate e 3.5 and ln 4.2 you would write the expressions math.exp(3.5) and math.log(4.2), respectively. This assumes that the math module has been imported. Now start practicing reading documentation. Go to https://docs.python.org/3/library/math. html to find the documentation of the math module. Next, look up math.exp and math.log on that page and read its description and usage. You already know how to call the log function given our example above, which makes now the best time to learn to read documentation! Documentation can seem cryptic to new programmers, so reading it when you already know how to call the function is an excellent way to learn the conventions used in documentation. Ask a member of the course staff if you want some help in interpreting the documentation.

Sanity check: With a max charging time of 0.5 seconds, it takes about 0.139 seconds to reach 7.5 volts (based on the given constants).

2.2 How long does the indicator light stay on?

Suppose that when the capacitor voltage is above a certain target voltage, the indicator light stays on. And when the voltage is below this target voltage, the light will stay turned off. (This cycle is repeated to make the indicator light blink.) Write a script. time on to determine the duration that the light stays on (during the period 0 ≤ t ≤ tf ) as a function of the charging time tc. Let the range of interest of the charging time to be 0.01 to 0.5 seconds. Solicit the target voltage as user input, re-prompting the user as necessary to ensure that the target voltage is a positive value. Your script. should produce a graph of duration versus charging time, an example of which is shown below for a target voltage of 5 volts.

Algorithmic notes:

❼ You need to loop over the charging times of interest. Choose a reasonable step size.

❼ For each charging time, you need to determine the duration when the light stays on (duration above the specified voltage). Again, use a loop to solve this subproblem—do not solve for the duration algebraically. Consider these two algorithms:

1. If the target voltage is achievable, loop over the time from zero to tf and check the voltage at each iteration. You need to devise a way to identify the beginning and ending time that the voltage is above the target voltage in order to calculate the duration.

2. Instead of going from t = 0 to the predetermined tf , you can have the loop end when the voltage drops back below the threshold.

You can use either algorithm to solve the problem, but which algorithm is more efficient? Write a one-sentence comment at the end of your script. to state and explain your answer.

Graphics notes. You will need the matplotlib.pyplot sublibrary for graphics, as before. However, it is not necessary to write code to “set up the figure window” as we did for the previous problems since we want to allow auto-scaling of the axes this time—matplotlib.pyplot by default will size the axes to show all the data, so you should neither manually set the axes scaling nor set the axes limits. I.e., just plot the data using a marker and a color of your choice. Label the axes and give the plot an appropriate title that includes the user-specified voltage.

Submit your files estimate area.py, time to target.py, and time on.py in CMS (after registering your group).

ZYNQ SD卡启动实验 立即下载

相关推荐

Rosenbrock 优化实验:GD、PSO 与 GA 的收敛对比matlab代码.rar

Rosenbrock 优化实验:GD、PSO 与 GA 的收敛对比matlab代码.rar

【FDE知识库】(1):AI 编程时代开发语言选型分析,云使用go,企业用java,算法python,底层c

AI 编程时代,开发语言选型需综合考量AI适配度、编译速度、跨平台与学习成本。Python以海量语料和即时执行成首选,适用于AI/ML与原型开发;TypeScript凭借类型系统与秒级构建,成为前端及应用层主流;Go以极速编译与统一风格,契合AI代理高效迭代需求。Rust/C++虽性能强,但编译慢、学习成本高,不适于快速试错。综合推荐:优先选Python、TypeScript、Go,兼顾效率与生态,实现最优AI协作体验。

freewebsys的专栏 656

用于居民用水定价和工业废水成本优化的MATLAB与LINGO模型.rar

用于居民用水定价和工业废水成本优化的MATLAB与LINGO模型.rar

全桥LLC谐振(变频+移相混合控制)变换器闭环仿真模型(Simulink仿真实现)

内容概要:本文详细介绍了全桥LLC谐振变换器采用变频与移相混合控制策略的闭环仿真模型构建与实现过程,重点阐述了其在Simulink环境下的系统建模、控制逻辑设计及闭环调节机制。通过融合频率调节与相位移动两种控制方式,实现对输出电压的高精度调控与高效能量转换,同时详尽展示了仿真模型的搭建步骤、关键参数设定以及反馈回路设计,确保系统具备优良的动态响应与稳定性。此外,文档还融入了状态估计与卡尔曼滤波等先进算法的应用,提升了系统的智能化水平与抗干扰能力,体现了该模型在现代电力电子系统研究中的综合性与前沿性。; 适合人群:具备一定电力电子与自动控制理论基础,熟悉MATLAB/Simulink仿真工具,从事电源设计、新能源变换技术研究的研发人员及高校研究生。; 使用场景及目标:①掌握全桥LLC谐振变换器的混合控制原理与仿真建模方法;②实现变频与移相控制的协同优化,提升变换器效率与动态性能;③应用于高频电源、电动汽车充电、可再生能源并网等领域的系统仿真与优化研究; 阅读建议:此资源以Simulink仿真为核心,不仅提供模型实现细节,更强调控制策略的设计思想与系统级优化思路,建议读者结合文档中的参考文献与配套代码,动手实践并调试模型,深入理解闭环控制参数对系统性能的影响。

电动汽车充电调度、到达行为以及考虑可再生能源的电价的MATLAB优化。.rar

电动汽车充电调度、到达行为以及考虑可再生能源的电价的MATLAB优化。.rar

生成式AI教师发展研究趋势数据集二十年

本数据集系统映射2006至2026二十年间生成式人工智能与智能体技术在教师专业发展领域的研究趋势、新兴主题与未来方向,适合教育技术、文献计量与学术趋势分析研究。生成式AI与智能体技术正在重塑教师培训、教研支持与课堂辅助的形态,本数据集将二十年跨度的相关研究整理为结构化记录,按时间脉络呈现研究主题的演化路径,可支撑主题建模、研究热点识别、跨年度趋势对比与前沿方向预测等分析任务。典型应用场景包括:教育技术领域的文献综述与系统性综述、科研选题与基金申请的趋势依据、教育政策研究的技术采纳分析、文献计量学与时序文本挖掘的方法教学。数据规模约12MB,结构规整,可直接配合Pandas、NLTK、Gensim等工具开展主题模型、关键词共现网络、时间序列趋势与可视化分析,也适合作为数字人文与教育研究课程的案例数据。重要说明:本数据为公开研究成果的整理汇编,分析结论应以原始文献为准。许可:CC0公有领域,可自由使用与二次分发。

哔哩哔哩概念-com,bilibili,app,blue-8,89,0.apk

哔哩哔哩概念版,armv7a架构, 版本号8,89,0.

T17_低空经济试验区十五五通用航空机场与无人机物流网设计方案_技术方案版.pptx

T17_低空经济试验区十五五通用航空机场与无人机物流网设计方案_技术方案版.pptx

大盘分时VWAP-看懂每天的资金底牌

看懂大盘的文章集合

AI政务大模型数字人平台规划方案(技术方案版).pptx

AI政务大模型数字人平台规划方案(技术方案版).pptx

git交互式学习系统(html)

git交互式学习系统(html)

全桥LLC谐振软开关Simulink仿真设计

内容概要:本文详细阐述了全桥LLC谐振软开关的Simulink仿真设计,重点聚焦于其闭环控制策略、关键参数设计及软启动过程的实现。以2.5kW双向全桥LLC变换器为例,深入剖析了变频与移相混合控制方法的协同应用,并提供了完整的仿真模型、设计报告与计算书,帮助读者系统掌握LLC谐振变换器在实现高效零电压/零电流开关(ZVS/ZCS)和维持稳定输出方面的核心技术与工程实践细节; 适合人群:具备电力电子技术基础,从事开关电源、新能源变换系统、高效率DC-DC变换器研发的工程师及高校相关专业的研究生; 使用场景及目标:①深入理解全桥LLC谐振变换器的工作原理与软开关的实现机制;②掌握基于Simulink的闭环控制系统建模、仿真调试与动态响应分析方法;③应用于高效率、高功率密度电源产品的设计、优化与性能验证; 阅读建议:建议读者结合提供的仿真模型与设计文档同步操作,重点关注谐振腔参数(如谐振电感、励磁电感、谐振电容)的匹配、控制环路(特别是变频与移相控制的协调)的设计以及系统的动态响应特性,通过反复的仿真验证与参数调整,深化对LLC拓扑性能边界与设计权衡的理解。

T14_低空经济校企合作共建机制与实施路径设计方案_可研报告版.pptx

T14_低空经济校企合作共建机制与实施路径设计方案_可研报告版.pptx

基于多策略改进蜣螂算法(MSDBO)的多无人机协同三维避障路径规划研究(Matlab代码实现)

内容概要:本研究聚焦于多无人机协同三维避障路径规划问题,提出了一种基于多策略改进蜣螂优化算法(MSDBO)的解决方案。该方法通过融合多种优化策略,有效提升了传统蜣螂算法在复杂三维空间中的搜索能力与收敛性能,解决了多无人机在执行任务过程中面临的路径规划难题。算法设计综合考虑了路径长度、飞行高度、环境威胁及转弯角度等因素,构建了以最低综合成本为目标的优化模型,并在Matlab平台上完成了仿真验证。实验结果表明,所提MSDBO算法相较于传统优化算法在路径质量、避障能力和计算效率方面均有显著提升,能够为多无人机系统在城市空中交通、应急救援等复杂场景下的协同飞行提供可靠的技术支撑。 适合人群:具备一定优化算法基础和Matlab编程能力,从事无人机控制、智能优化、路径规划等相关领域的科研人员及研究生。 使用场景及目标:①应用于多无人机集群在复杂三维环境中的协同避障路径规划;②为智能交通、智慧城市建设、灾害巡检等实际工程问题提供高效路径优化方案;③推动智能优化算法在无人系统中的理论研究与实践应用。 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注目标函数构建、约束处理机制及多策略改进部分,并可通过调整环境参数和无人机数量进行扩展性实验,进一步掌握算法的适应性与鲁棒性。

增材制造基于冷喷涂固态成型的金属3D打印技术:零氧化铜铝复合与航空修复应用

内容概要:本文系统阐述了固态金属成型技术——冷喷涂增材制造(CSAM)的核心原理、工艺机制与工程应用。CSAM通过将金属粉末以超音速撞击基体,在不熔化材料的前提下实现固态冶金结合,具有近乎零氧化、热影响区小、适用于铜铝钛及异种金属复合制造等优势。文章深入解析其结合机制(如绝热剪切失稳、氧化膜破裂与新鲜金属接触)、关键工艺参数(气体选择、温度、速度、氧含量)、复合致密化手段(激光辅助、原位微锻造、热处理等),并展示了在新能源汽车散热板、航空部件修复、金属基复合材料等领域的实际应用。同时指出了当前存在的塑性低、精度不足、成本高等痛点及其工程对策,并展望了大型化、多材料集成、全链条数字化与绿色制造的发展趋势。; 适合人群:从事增材制造、材料工程、金属加工及相关领域研发的技术人员,具备一定材料科学与制造工艺背景的科研人员和工程师。; 使用场景及目标:①理解冷喷涂与其他金属3D打印技术的本质差异与互补关系;②掌握CSAM在异种金属连接、热敏感部件修复、高性能涂层制备中的应用潜力;③指导工艺优化、材料选型与工业落地路径设计; 阅读建议:本文兼具理论深度与工程实践视角,建议结合图表与案例重点研读结合机制、复合工艺与应用场景部分,并关注氧含量控制、数字孪生与标准化趋势,以推动技术从实验室向规模化生产转化。

政务服务AI知识库平台规划方案(生态合作版).pptx

政务服务AI知识库平台规划方案(生态合作版).pptx

14-15日周末听写音频.mp3

14-15日周末听写音频.mp3

化学和超级电容混合储能双层容量配置平抑风电波动,采用RLMD分解风电功率,粒子群算法求解(Matlab代码实现)

内容概要:本文提出一种针对风电功率波动的混合储能系统双层容量配置方法,采用化学储能(如锂离子电池)与超级电容器构成混合储能系统,以充分发挥前者高能量密度与后者高功率密度及快速响应的优势。为实现精细化功率分配,引入可重复局部均值分解(RLMD)对风电功率序列进行多尺度分解,有效分离出不同时间尺度的波动分量,据此制定协调控制策略:高频波动由超级电容承担,低频波动由化学储能承担,从而减轻单一储能单元的压力,延长整体系统寿命。在此基础上,构建了一个双层优化模型:上层模型以系统全寿命周期成本最低和运行稳定性最高为优化目标,确定储能单元的最优额定容量;下层模型则以实时功率分配损耗最小为目标,实现储能间的动态功率分配。整个优化问题采用粒子群优化算法(PSO)进行高效求解,并通过Matlab代码实现了完整的仿真验证。仿真结果表明,该方法不仅能显著平抑风电并网带来的功率波动,改善电能质量,还能有效降低系统综合成本,并通过合理分配充放电任务,减少化学储能的循环次数,延长其使用寿命。; 适合人群:具备电力系统、新能源控制或优化算法基础,从事风电并网、储能系统设计与优化等相关领域的科研人员及工程技术人员。; 使用场景及目标:①解决风电功率波动对电网稳定性的影响;②优化混合储能系统中电池与超级电容的容量配置,实现成本与性能的平衡;③掌握RLMD信号分解与粒子群算法在能源系统优化中的应用; 阅读建议:此资源结合了信号处理、储能控制与智能优化算法,建议读者在学习过程中重点关注RLMD分解原理、双层优化模型的构建逻辑及粒子群算法的实现细节,并结合提供的Matlab代码进行仿真复现与参数调试,以深入理解各模块的协同机制。

上一篇: COMP3331/9331 Computer Networks and Applications
下一篇: COMP2240/COMP6240 - Operating Systems
vx___codinghelp
博客等级 码龄2年 778粉丝 101原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值