Multiple outputs in function not showing.

3 次查看(过去 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

回答(1 个)

Steven Lord
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)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by