help with matlab error

Just need help with the problem
1st part of code is in the attahcment and heres the second part
I am getting an error that says "variable reshas an incorrect value" and "output arguement xroot and maybe others not assigned to during call to PA_secant" and "not enough output arguements"

8 Comments

Attaching a picture is never useful than attaching code/code file.
If i==imax the first iteration of the loop, then the break will be taken before anything is assigned to xroot or residual.
function [xroot,residual,ea,iterCount] = PA3_secant(fun,xi,es,max_it,varargin)
%copy/paste the commands for your function here. You can change the names,
%but not the order, of the input and/or output variables in the function
%command to match your code. Do not change the name of the function,
%however, as this exact name (i.e. "PA3_secant") is required for
%the test suite to run.
f=@(x) fun(x);
if (nargin<3)
e = 1e-5
else e=es;
end
x0=xi(1);
x1=xi(2);
i=0;
if (nargin<4)
imax=30;
else imax=max_it;
end
x2=x1+1;
flag=1
while (abs(f(x2))>e)
if(i~=0)
x1=x2;
end
x2=x1-((f(x1)/(f(x1)-f(x0)))*(x1-x0));
x0=x1;
i=i+1;
if (i==imax)
flag=0;
break;
end
if (flag==1)
xroot=x2
else xroot=[];
end
iterCount=i;
residual=f(xroot);
ea=abs((x2-x1)/(x2));
end
So what do you reccomend that I do?
What are the input values you are testing with?
used for the first three tests
f = @(x) sin(2*x) - tan(x/4);
[xr,res,ea,iter] = PA3_secant(f, [1 1.1], 1e-3, 50);
this is the problem statement
Write a function m-file that implements the secant method. Your function should accept the following inputs (in order):
  1. A function defining the roots problem
  2. A vector of two initial guesses (first element is the first guess, second element is second guess)
  3. A stopping criterion ( es ) for the numerical solution with a default value of 1e-5
  4. A maximum iteration count for the numerical solution with a default value of 30
  5. An arbitrary number of parameter values associated with the roots problem
Your function should output the following four scalar outputs (in order):
  1. The root estimate
  2. The residual in the numerical solution
  3. The approximate relative error in the numerical solution
  4. The number of iterations required for convergence
When I run your code with those parameters, I do not get any error message.
"Variable res has an incorrect value" looks like it might be from the automatic grading system.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 11 May 2019
Edited: Cris LaPierre on 11 May 2019

0 votes

It looks like your teacher is using MATLAB Grader for this problem. Because of variable scope in a function, MATLAB Grader is calling your function with inputs your teacher defines, and assessing the outputs. It can not check any other variables. The outputs are captured in variables your teacher has named, which is why you see an error message about a variable your code does not create.
It would appear the output variable they named res is not being assigned the expected value by your function. The inference, then, is that your function has not been properly implemented.
I think it's safe to assume res = residual. Check how you are calculating your residual. Compare to your notes from class. If there is any feedback provided in this assessment test, use that to help find your mistake.

Communities

More Answers in the  Distance Learning Community

Categories

Asked:

on 25 Apr 2019

Edited:

on 11 May 2019

Community Treasure Hunt

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

Start Hunting!