How do I plot a function with multiple outputs?

11 次查看(过去 30 天)
I am trying to create a function that will plot with the following parameters:
[VVectA3,VVectB3] = rwAB(10,0,0,0.3,0.3,1);
figure;
plot(VVectA3)
hold on
plot(VVectB3)
plot(VVectA3+VVectB3)
For my rwAB function this is what I did:
function [VvectA,VvectB]=rwAB(nTrials,VA,VB,alphaA,alphaB,lambda)
VVectA = VA;
VVectB = VB;
for i = 1:nTrials
VA = rwABRule(VA,alphaA,lambda)
VB = rwABRule(VB,alphaA,lambda)
VVectA = [VVectA VA]
VVectB = [VVectB VB];
end
end
Furthermore, for the function rwABRule that is within my rwAB function I did this:
[VA,VB]=rwABRule(VA,VB,alphaA,alphaB,lambda)
function [VA,VB]=rwABRule(VA,VB,alphaA,alphaB,lambda)
VA = VA + alphaA*(lambda-VA)
VB = VA + alphaB*(lambda-VB)
end
However, when I try to plot it I am given these three errors:
Error:
Local function name must be different from the script name.
Error:
VA = rwABRule(VA,alphaA,lambda)
Error:
[VVectA3,VVectB3] = rwAB(10,0,0,0.3,0.3,1)
So I know that there is something wrong with either my rwABRule function or my rwAB function or both but I can not figure out what I am doing wrong. I thought I was creating my function correctly but I guess not. Any help would be greatly appreciated.

采纳的回答

Dyuman Joshi
Dyuman Joshi 2022-7-30
You are calling the function rwABRule incorrectly.
Also, there's a spelling mistake in your function call.
[VVectA3,VVectB3] = rwAB(10,0,0,0.3,0.3,1);
figure;
plot(VVectA3)
hold on
plot(VVectB3)
plot(VVectA3+VVectB3)
function [VVectA,VVectB]=rwAB(nTrials,VA,VB,alphaA,alphaB,lambda)
%capital V^
VVectA = VA;
VVectB = VB;
for i = 1:nTrials
[VA,VB] = rwABRule(VA,VB,alphaA,alphaB,lambda);
%corrected function call
VVectA = [VVectA VA];
VVectB = [VVectB VB];
end
end
function [VA,VB]=rwABRule(VA,VB,alphaA,alphaB,lambda)
VA = VA + alphaA*(lambda-VA);
VB = VA + alphaB*(lambda-VB);
end
  6 个评论
Dyuman Joshi
Dyuman Joshi 2022-7-30
编辑:Dyuman Joshi 2022-7-30
%y limit
ylim([0 1])
%color
plot(VVectA3, 'r')
plot(VVectB3, 'b')
plot(VVectA3+VVectB3, 'k')
You can choose color of your choice

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by