Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

midpoint rule for matlab

Date: 2022-12-14 11:40:39

Hello I was asked to create a matlab code for the midpoint rule. What I have is the code for eulers method, so I have to make some modifications, but I am struggling to do it I have the following 

 

function H = heun(f,a,b,ya,M) h = (b-a)/M; T = zeros(1,M+1); Y = zeros(1,M+1); T = a:h:b; Y(1) = ya; for j = 1 : M k1=feval(f,T(j),Y(j)); k2=feval(f,T(j+1),Y(j)+h*k1); Y(j+1)=Y(j)+(h/2)*(k1+k2); end H = [T' Y']; function f = dif1(t,y) f=(t-y)/2;

So yea my function is y'=(t-y)/2 and the interval and other stuff i define in the command window..

If it is possible to make the for loop into a mid point rule I think that is the way to go, any help would be appreciated. 

Expert Answer:

Below is an implementation in MATLAB I have done of the Euler's Method for solving a pair of coupled 1st order DE's. It solves a harmonic oscillator of represented by the following:

y1(t+h) = y1(t) + h*y2(t)

y2(t+h) = y2(t) + h*(-A/M y1(t) -B/M y1(t)/|y1(t)|) 

 

% Do the integration using the Euler Method while(T<=T1) % Update the position of the pt mass using current velocity Y1(i+1) = Y1(i) + H*Y2(i); % Update the velocity of the pt mass checking if we are turning % within the C/A band if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) ) Y2(i+1) = Y2(i); else Y2(i+1) = Y2(i) + H * ( ((-A/M)*Y1(i)) - (B/M)*sign(Y2(i)) ); end % Incriment the time by H T = T + H; % Increase the looping index variable i = i + 1; end

Without explicitly solving YOUR homework question, I hope this example helps. A few things to note: the if statement is accounting for static friction in this particular example -- so disregard that and only look at what happens after the 'else'.

Also note that I have initial conditions y1(0) and y2(0) defined as Y1(i=1) and Y2(i=1), so starting with Yj(i+1) gives Yj(2). Note that T1 is the ending time of the simulation.

Below is the same problem using the Improved Euler Method. If you derive the update equations for this system you should be able to walk through the code. 

 

% Do the integration using the Improved Euler Method % Ki_j = K^i_j while(T<=T1) % Calculate K^i_j's K1_1 = Y2(i); % Must check if we are turning within C/A if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) ) K1_2 = 0; else K1_2 = (-A/M)*Y1(i) - (B/M)*sign(Y2(i)); end K2_1 = Y2(i)+H*K1_2; % Checking if we are turning within C/A if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) ) K2_2 = 0; else K2_2 = (-A/M)*(Y1(i) + H*K1_1) - (B/M)*sign(Y2(i)+ H*K1_2); end % Update the position and velocity Y1(i+1) = Y1(i)+ (H/2)*(K1_1+K2_1); Y2(i+1) = Y2(i) + (H/2)*(K1_2+K2_2); % Incriment the time by H T = T + H; % Increase the looping index variable i = i + 1; end

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com