COSC2500/COSC7500—Semester 2 2024R

Java Python COSC2500/COSC7500—Semester 2, 2024

Exercises—due 3:00 pm Friday 30th August

Required

The grade of 1–5 awarded for these exercises is based on the required exercises. If all of the required exercises are completed correctly, a grade of 5 will be obtained.

R1.1 For the polynomial f (x)  = x4 − 2x3, calculate the derivative and the 2nd derivative analytically (the first derivative so that you can compare with your numerical derivatives to determine the error, and the second deriva- tive might be useful for understanding your results—see equations (1.4) and (1.6)).

a) Choose different step sizes hand plot a log-log graph of error vs step size to compare the numerical derivatives found using the forward, backward,and central difference formulas with the actual derivative. How accurate are the numerical derivatives at x = 0, 1, 1.5, and 2? At what step sizes do you obtain the minimum errors, and what are the minimum errors (see Sauer figure 5.1)? Do you expect that this would be a problem? What if you were using single-precision floating point? (You can easily try this in Matlab by using the single() function.)

b) Add some additional error to your polynomial.  For example, try y  = f(x)  +  0.001  *  randn(size(x));. How does the error in the numer- ical derivative vary with step size h? Try changing the size of the error.

Checklist

Did you differentiate the function (including with added error)? Did you explain how you did it?

Did you plot graphs showing the error vs step size at each point? Are your graphs and axes properly labelled?

Did you discuss whether the results were what you expected? Did you discuss any unusual results?

Did you include code where appropriate?

Did you reference the source of any code you didn’twrite?

R1.2 Compare the trapezoid rule and Simpson’s rule for integrating cos(x) from x = 0 to x = π/2 and x = π, and sin(x2) + 1 from x = 0 to x = 10. You can use the Matlab function trapz and the Simpson’s method function SIMP42 from Blackboard, or write your own code, or use other code.

a) Plot a log–log graph to compare the accuracy of the trapezoid rule and Simpson’s rule for different step sizes.

Hint: Note that only certain step sizes are allowed. Rather than choosing the step size, choose the number of points, and calculate the step size from that.

Note that sin(x2) + 1dx ≈ 10.58367089992962334

b) Compare the accuracy of an adaptive step size integrator (e.g., integral() or quad() in Matlab).

c) How does additional error affect integration? Try it and see, using the same random error term you used before.

d) How much does round-off error affect integration? Since a small value of h means that we need many points, we also increase our require- ments in time and memory. Do the time and/or memory required be- come excessive before we run into any problems with round-off error?

WARNING: Using too much memory can cause Matlab and/or your computer to crash or become unrespnsive. Don’t increase the number of steps too quickly when trying this (doubling the number of steps should be OK), and make sure you have saved your work.

Checklist

Did you integrate the functions using appropriate integrators? Did you explain how you did it?

Did you plot graphs showing the error vs step size for each function? Are your graphs and axes properly labelled?

Did you discuss whether the results were what you expected? Did you discuss any unusual results?

Did you discuss the effect of additional error?

Could you see any effect due to round-off error? Did you discuss this? Did you include code where appropriate?

Did you reference the source of any code you didn’twrite?

R1.3 Integration over discontinuities. Integrate the function

from x = 0 to x = 3, for varying step sizes, using the trapezoidal method. Plot the error as a function of step size. A Matlab function calculating this function, f_r13a.m, is available on Blackboard.  Note that the integral is equal to 2e − 2.

Don’t just integrate from x = 1 to x = 2! We want to see what integrating over the discontinuities does!

Checklist

Did you integrate the function for varying step sizes? Did you explain how you did it?

Did you plot graphs showing the error?

Are your graphs and axes properly labelled?

Did you discuss whether the results were what you expected? Did you discuss any unusual results?

Did you include code where appropriate?

Did you reference the source of any code you didn’twrite?

R1.4 Integration over an infinite interval. Integrate exp(−x) from x = 0 to x = ∞ , by numerically integrating from x = 0 to increasing values of x. Plot a graph of the error vs the value of x you integrate to. Compare results for a fixed step size and a fixed number of steps.

Checklist

Did you integrate the function to increasing values of x? Did you explain how you did it?

Did you plot graphs showing the error, for fixed step size and step number? Are your graphs and axes properly labelled?

Did you discuss whether the results were what you expected? Did you discuss any unusual results?

Did you include code where appropriate?

Did you reference the source of any code you didn’twrite?

R1.5 The intensity of a dougnut-shaped (in cross-section) laser beam was mea- sured. The data had the background noise subtracted, was smoothed, and left and right side data (which should be identical) was averaged, giving the intensity as a function of radius:

The xy data is:

x = [ 0 0.1341 0.2693 0.4034 0.5386 0.6727 ...

0.8079 0.9421 1.0767 1.2114 1.3460 1.4801 ...

1.6153 1.7495 1.8847 2.0199 2.1540 2.2886 ...

2.4233 2.5579 2.6921 2.8273 2.9614 3.0966 ...

3.2307 3.3659 3.5000 ];

y = [ 0 0.0310 0.1588 0.3767 0.6452 0.8780 ...

0.9719 1.0000 0.9918 0.9329 0.8198 0.7707 ...

0.8024 0.7674 0.6876 0.5937 0.5778 0.4755 ...

0.3990 0.3733 0.2870 0.2156 0.2239 0.1314 ...

0.1180 0.0707 0.0259 ];

Note that the data has been re-scaled for the graph shown; your graph will have different values but should have the same shape.

Provide a simple approximation to this data. The one obtained in the actual real-life case is shown below; this was a piece-wise polynomial approxima- tion. You can use either equation (1.15) or trial-and-error and your own judgment to find the best order of polynomial to use.

Checklist

Did you fit a suitable function or functions to the data?

Did you include a graph comparing your fit to the data?

Did you explain how you chose the best fit?

Did you include a graph or fit measure values illustrating your choice?

Did you include code where appropriate?

Did you reference the source of any code you didn’t write?

Additional

Attempts at these exercises can earn additional marks, but will not count towards the grade of 1–5 for the exercises. Completing all of these exercises does not mean that 6 marks will be obtained—the marks depend on the quality of the answers. It is possible to earn all 4 marks without completing all of these additional exercises.

A1.1 Calculate the 2nd derivative and higher derivatives off (x) = 1 + x + x2 + x3 + x4 + x5 + x6 + x7 + x8 using the finite difference formulae for higher derivatives. How do the errors vary with step size?

A1.2 How would you calculate the integral

where α varies from approximately 0.1 to 1, from β = 0.01 to β = 100? What difficulties do you expect?

Calculate this integral for various combinations of α and β .

A1.3 Estimate the error in a numerical derivative and/or integral from the con- vergence of the solution as the step size is changed.  (It’s best to use step sizes that are large enough to avoid round-off error.)  Compare your esti- mate of the error with actual error determined from comparison with the analytical solution, if an analytical solution is available.

A1.4 While Matlab is an interpreted language, and therefore loops can be slow, operations on matrices and vectors can be much faster. Matlab code is often written to make use of vector operations instead of loops (“vectorisation”); this can lead to code that is fast, but requires much more memory. Compare the memory and time requirements for numerical integration in Matlab us- ing vector operations, Matlab using loops, and a compiled language such as C using loops.

A1.5 Compare polynomials and real Fourier series (i.e., sine–cosine series) for fitting the data in a15data . m. Matlab code for calculating/fitting a Fourier series is available on Blackboard:  sine_cosine_fit_int .m, which uses a Fourier integral, and sine_cosine_fit_ls.m, which uses a least-squares fit.

A1.6 Write a program to calculate the increase in the viscous drag coefficient due to a wall using the approximation given by Chaoui and Feuillebois (2003).

A1.7 How are the special functions available on a scientific calculator calculated? You might like to write some code to calculate these to test the efficiency and accuracy of the methods.

A1.8 Consider the algorithm for the calculation of Wigner 3jfunctions described by Brock (2001). Note that there are a number of special cases. How impor- tant is it to treat these cases separately? AMatlab implementation of Brock’s algorithm, wigner3j . mis available on the course website. (If you compare results with the examples given in Brock (2001), note that the “baseball” calculation is wrong, due to integer overflow.)

Programming hints

R1.1 Since you want to look at the effect of step size on error, you want to find the numerical derivatives for a many step sizes, over a large range. It’s useful to use logarithmically spaced points to make a vector of step sizes. We have many different ways to make a vector of, e.g., step sizes.  The three main ones are:

Linear spacing, with a specified spacing:

h  = start_val:spacing:end_val;

Linear spacing, with a specified number of points:

h  = linspace(start_val , end_val , num_points);

Logarithmic spacing, with a specified number of points:

h  = logspace(start_power , end_power , num_points);

Note that we don’t specify the start and end values, but the powers of 10.

In this case, the third one is our simplest choice.

Rather than hard-coding the function you are differentiating in your main script. or function, it’s often useful to make a separate m-file for that func-tion, or use an inline function. Very useful when you want to change it, since there’s only one place where you have to make changes! If we wanted a separate m-file to calculate y = x2 exp(x), we could put the following:

function y = f12(x) y = x . ^2  . * exp(x); return

in an m-file f12 . m (the function name and the m-file name need to be the same). In the first line, we tell Matlab that this is a function (rather than a script), say that there is one output variable (y), and one input parameter (x).

If our function is simple enough to have on one line, we might use an in-line function instead. E.g.,

f12 = @(x) x . ^2 . * exp(x);

Here, f12 is a function handle (like a pointer or address). The @ op- erator creates the handle. For more on in-line functions, see Ap- pendix B of Sauer or http://au . mathworks . com/help/matlab/matlab_ prog/anonymous-functions . html

Next, you need to cycle through the different step sizes. It’s much easier to do this automatically in a loop rather than choosing different step sizes by hand each time. Your code might look something like:

x = 1;

num_points = 1000;

h = logspace(-20,-1,num_points);

for n = 1:num_points

%  Forward difference

fd = ( f12(x+h(n)) - f13(x) ) / h(n);

%  Exact result

ed = f12exact(x);

%  Forward difference error

fde(n) = abs( fd - ed );

end

figure;

loglog(h,fde);

title([’x␣=␣’ num2str(x)]); ylabel(’Error’);

xlabel(’Step␣size␣h’);

R1.2 To use integral, you need to consider two points:

Unlike trapz, where the input is vectors of x and y values, the input to integral is a function handle, and start and end x values for the integral. If you have your function in an m-file f13.m, then you need to use the @ operator to produce a function handle for quad:

integral13  = integral(@f13 , start_val , end_val);

If you used an inline function for f15, it’s already a function handle, and

integral13  =  integral(f13 , start_val , end_val);

works.

You choose how many points trapz and SIMP42 use, while integral chooses its own number of points. So how can you compare the errors? A simple solution is to use quad instead of integral; quad will return a 2nd output, which is the number of points it used.

R1.5 It’s useful to plot your fitted curve over a much finer grid of x values rather than just using the original x values.  For example, after using polyfit to find your polynomial:

x2  =   linspace (0,3 . 5,1000);

y2  =  polyval(P, x2);

plot(x,y,’*’, x2, y2);

Now you can see what your fitted curve does between the original data points         

使用优化算法,以优化VMD算法的惩罚因子惩罚因子 (α) 和分解层数 (K)。 1、将量子粒子群优化(QPSO)算法与变分模态分解(VMD)算法结合 VMD算法背景: VMD算法是一种自适应信号分解算法,主要用于分解信号为不同频率带宽的模态。 VMD的关键参数包括: 惩罚因子 α:控制带宽的限制。 分解层数 K:决定分解出的模态数。 QPSO算法背景: 量子粒子群优化(QPSO)是一种基于粒子群优化(PSO)的一种改进算法,通过量子行为模型增强全局搜索能力。 QPSO通过粒子的量子行为使其在搜索空间中不受位置限制,从而提高算法的收敛速度与全局优化能力。 任务: 使用QPSO优化VMD中的惩罚因子 α 和分解层数 K,以获得信号分解的最佳效果。 计划: 定义适应度函数:适应度函数根据VMD分解的效果来定义,通常使用重构信号的误差(例如均方误差、交叉熵等)来衡量分解的质量。 初始化QPSO粒子:定义粒子的位置和速度,表示 α 和 K 两个参数。初始化时需要在一个合理的范围内为每个粒子分配初始位置。 执行VMD分解:对每一组 α 和 K 参数,运行VMD算法分解信号。 更新QPSO粒子:使用QPSO算法更新粒子的状态,根据适应度函数调整粒子的搜索方向和位置。 迭代求解:重复QPSO的粒子更新步骤,直到满足终止条件(如适应度函数达到设定阈值,或最大迭代次数)。 输出优化结果:最终,QPSO算法会返回一个优化的 α 和 K,从而使VMD分解效果最佳。 2、将极光粒子(PLO)算法与变分模态分解(VMD)算法结合 PLO的优点与适用性 强大的全局搜索能力:PLO通过模拟极光粒子的运动,能够更高效地探索复杂的多峰优化问题,避免陷入局部最优。 鲁棒性强:PLO在面对高维、多模态问题时有较好的适应性,因此适合海上风电时间序列这种非线性、多噪声的数据。 应用场景:PLO适合用于优化VMD参数(α 和 K),并将其用于风电时间序列的预测任务。 进一步优化的建议 a. 实现更细致的PLO更新策略,优化极光粒子的运动模型。 b. 将PLO优化后的VMD应用于真实的海上风电数据,结合LSTM或XGBoost等模型进行风电功率预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值