Multiple outputs in function not showing.
조회 수: 4 (최근 30일)
이전 댓글 표시
I'm trying to write a function for calculating the fixed point iteration for a system of 2 equations. Although my function works it only displays the x-output. My code is below:
%%Solves a system of 2, 2 variable equations using the fixed point
%%iteration method.
%Fx=function of two variables of the form x=g(x,y)
%Fy=function of two variables of the form y=g(x,y)
%Xest=initial estimate for x-value of root
%Yest=initial estimate for y-value of root
%imax=number of iterations
%Xs=output of FixedIterationRoot, stores all values of x.
%Ys=output of FixedIterationRoot, stores all values of y.
function [Xs , Ys]= FixedIterationRoot_sys2(Fx,Fy,Xest,Yest,imax)
Xi(1)=Xest;
Yi(1)=Yest;
syms x y
dFx=diff(Fx,x);
dFy=diff(Fy,y); %finds derivative of Fun
if abs((subs(dFx,Xest))+abs(subs(dFy,Yest)))>=1 %Stating the condition of convergence.
disp('Error: algorithm does not converge')
return
else
for i=2:imax % Calculating the solutions to fixed point equation.
Xi(i)=subs(Fx,[x,y],[Xest,Yest]);
Yi(i)=subs(Fy,[x,y],[Xest,Yest]);
Xest=Xi(i);
Yest=Yi(i);
end
end
Xs=Xi;
Ys=Yi;
end
댓글 수: 0
답변 (1개)
Steven Lord
2017년 4월 26일
Defining a function to return multiple outputs is necessary but not sufficient to have that function return multiple outputs. You also need to call it with multiple outputs.
For instance, the max function is defined to return up to two outputs. If you ask it for one, it will return one. If you ask it for two, it will return two. If you ask it for three, it will throw an error.
x = 1:10;
theMaximum = max(x)
[theMaximum2, location2] = max(x)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!