MATLAB Answers

Assigning values to variables "Method" and "Value"

1 view (last 30 days)
Kyle Donk
Kyle Donk on 3 Feb 2020
Answered: KSSV on 4 Feb 2020
Hi all. I am working on a problem where I have three saved functions: Mid, Trap, and Simp. These functions are used to approximate an integral in function "Func". Basically, I have to end up with a table that has an N value, the method (Mid, Trap, or Simp) used to find that N value, the value that each function brings forth for every value N, and the absolute error for each function calculating each number. However, I am not sure how to define Method and Value. Method would have to technically equal three different things, and value would have to be defined in a way that it is calculating the value for each function at each N. Could you guys steer me in the right direction? (This is a school assignment, so no complete answers).
Here is my code so far:
%Approximating an Integral using multiple methods
fprintf('%s | %s | %s | %s \n','N','Method','Value','abserror')
a=0;
b=2;
N=0;
Value=0;
Method=[];
for N=[10 20 40 80 160 320]
m=Mid(a,b,N);
abserror=abs(pi-m);
t=Trap(a,b,N);
abserror=abs(pi-t);
s=Simp(a,b,N);
abserror=abs(pi-s)
fprintf('%s | %s | %s | %s \n',N,Method,Value,abserror)
end

  0 Comments

Sign in to comment.

Answers (1)

KSSV
KSSV on 4 Feb 2020
%Approximating an Integral using multiple methods
fprintf('%s | %s | %s | %s \n','N','Method','Value','abserror')
a=0;
b=2;
N=[10 20 40 80 160 320] ;
Method = zeros(length(N),1) ;
Value = zeros(length(N),1) ;
abserror = zeros(length(N),1) ;
for i = 1:length(N)
m=Mid(a,b,N(i));
abserror(i)=abs(pi-m);
t=Trap(a,b,N(i));
abserror(i)=abs(pi-t);
s=Simp(a,b,N);
abserror(i)=abs(pi-s)
end

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!