Need help writing matlab code with given pseudocode
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
Given Problem:
Exercise 2: Loss of Significance
• Suppose that you define b = a(1+epsilon). Write a function that calculates magnitude of relative and absolute errors in epsilone when 'a' and epsilon are introduced. The loss of significance is defined by first calculating 'b', and then back‐ calculating for known values of 'a' and 'b' .
• If a = pi and epsilon = 10^-j, then use this function to create plots of the magnitude of relative and absolute errors for varying j.
Given pseudo-code:
Exercise 2: Loss of Significance
1. Main function
a. Define a 1x10 array for the magnitude of relative error
b. Create a loop for ݆ j = 1: 10
i. Define epsilon
ii. Call the subfunction that determines the magnitude of absolute and relative
errors for a = pi and epsilon
c. Create plots of the magnitude of absolute and relative errors vs. ݆
2. Subfunction
a. Inputs: a and epsilon
b. Outputs: magnitudes of absolute and relative error
c. Calculate b = a(1+epsilon)
d. Calculate epsilon based on a and b. Call this parameter epsilon prime.
.
e. Determine the magnitudes of absolute and relative error by comparing the calculated epsilon Prime
to the “true” value of epsilon.
My attempt:
function LossOfSignificance()
clc;
clear;
RelError = zeros(1,10)
for j = 1:10
MyFun(pi, 3e-5)
epsilon = epsilon*10^(-j)
end
end
function MyFun(a,epsilon)
a;
epsilon
b = a*(1+epsilon);
epsilonPrime = (b-a)/a;
bPrime = a*(1+epsilonPrime);
AbsErrorPrime = abs(bPrime -a);
RelErrorPrime = abs(AbsErrorPrime/a);
end
My output error:
??? Undefined function or variable "epsilon".
Error in ==> LossOfSignificance at 8
epsilon = epsilon*10^(-j)
I'm not very experienced with Matlab. Please help. Thanks.
0 commentaires
Réponses (2)
Walter Roberson
le 29 Jan 2017
epsilon should not be 10^-j times its previous value: it should just be that calculation
0 commentaires
Steven Lord
le 30 Jan 2017
As written, the variable epsilon only exists in the workspace of the MyFun function. When the function finishes executing, its workspace (and all the variables the workspace contains) are destroyed. If you want variables created inside MyFun to exist outside, define them as output arguments to your function in the definition:
function AbsErrorPrime = MyFun(a,epsilon)
or
function [AbsErrorPrime, RelErrorPrime] = MyFun(a,epsilon)
and call the function with one or more outputs inside LossOfSignificance.
[AEP, REP] = MyFun(pi, 3e-5) % Assuming MyFun is defined to return two outputs
0 commentaires
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!