Here is the code:
clear all;a = 1;b = 2;alpha = 2;format longG;[w,t] = RungeKutta(a,b,0.25,alpha);combinedArray = [(0:4)' t' w'];T3 = array2table(combinedArray);T3.Properties.VariableNames = {'i','t_i','w_i'};filename1 = 'Table1.csv';writetable(T3,filename1);figure('DefaultAxesFontSize',16);plot(t,w,'LineWidth',2);xlabel('t');ylabel('y');NArray = zeros(1,5);hArray = zeros(1,5);errorArray = zeros(1,5);hFourthArray = zeros(1,5);for k = 1:5 h = 1/(2^k); N = (b-a)/h; [w,t] = RungeKutta(a,b,h,alpha); error = abs(w(N+1) - TrueSolution(t(N+1))); NArray(k) = N; hArray(k) = h; errorArray(k) = error; hFourthArray(k) = h^4; end combinedArray2 = [hArray' errorArray'];T4 = array2table(combinedArray2);T4.Properties.VariableNames = {'h','Error'};filename2 = 'Table2.csv';writetable(T4,filename2);figure('DefaultAxesFontSize',16);loglog(NArray,errorArray,NArray,hFourthArray,'LineWidth',2);xlabel('n');legend('Error','10h^4');
Best Answer