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

madhan ravi
madhan ravi 2019년 4월 25일
Attaching a picture is never useful than attaching code/code file.
Walter Roberson
Walter Roberson 2019년 4월 25일
If i==imax the first iteration of the loop, then the break will be taken before anything is assigned to xroot or residual.
Tony Vo
Tony Vo 2019년 4월 25일
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
Tony Vo
Tony Vo 2019년 4월 25일
So what do you reccomend that I do?
Walter Roberson
Walter Roberson 2019년 4월 25일
What are the input values you are testing with?
Tony Vo
Tony Vo 2019년 4월 25일
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);
Tony Vo
Tony Vo 2019년 4월 25일
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
Walter Roberson
Walter Roberson 2019년 4월 25일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2019년 5월 11일
편집: Cris LaPierre 2019년 5월 11일

0 개 추천

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.

커뮤니티

더 많은 답변 보기:  원격 교육 커뮤니티

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2019년 4월 25일

편집:

2019년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by