Can you write comment lines and use matlab function and subfunction? Question: P
ID: 3721910 • Letter: C
Question
Can you write comment lines and use matlab function and subfunction? Question: Please convert the given code to a Matlab program. Please read your book for detailed explanation (Page 72) 1. In your program, please also calculate true percent relative error 2. Discuss your results in a comment section of your program 3. For Bonus Points: Please make sure that summation is performed with a matlab function and subfunction. PROGRAM fig0312 MPLICIT none FIGURE 3.12 Fortran 90 program to sum a number 10s times. The case sums the number 1 in single precision and the number 10 in single ansum2-0 double precision REAL::Suml, sum2, x1, x2 DOUBLE PRECISTON::Sum3, x3 x2-1.e-5 x3-1.d-5 DO i-1,100000 uml sum1+x sum2 sum2+x2 sum3-sum3+x3 END DO PRINTm PRINT sum2 PRINT, *· sunn3 00000.000000 1.000990 .999999999980838E-001Explanation / Answer
Hi ,
As per the problem statement, I wrote the code in matlab,
Note : In this problem statement , double precision is same exp in matlab, so both sum2 and sum3 are same results
Please find the code below:
RelativeErrorFind.m:
% Program to find the Relative Error
% Given Relative Error = True Erro/True Value *100%
clear all;
x1 = 1;
x2 = 1*exp(-5);
x3 = 1*exp(-5); %Double precision assignment value is equal to exp
sum1 = 0;
sum2 = 0;
sum3 = 0;
i =1;
while (i < 10^5)
sum1 = sum1 + x1;
sum2 = sum2 + x2;
sum3 = sum3 + x3;
i= i+1;
end
disp(sum1);
disp(sum2);
disp(sum3);
Please find the sample output below:
Thanks
% Program to find the Relative Error
% Given Relative Error = True Erro/True Value *100%
clear all;
x1 = 1;
x2 = 1*exp(-5);
x3 = 1*exp(-5); %Double precision assignment value is equal to exp
sum1 = 0;
sum2 = 0;
sum3 = 0;
i =1;
while (i < 10^5)
sum1 = sum1 + x1;
sum2 = sum2 + x2;
sum3 = sum3 + x3;
i= i+1;
end
disp(sum1);
disp(sum2);
disp(sum3);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.