MATLAB plotting function not enough input arguments - matlab

I am currently taking Andrew Ng's Stanford Machine learning course and am using MATLAB to complete the programming assignments despite being new to the MATLAB programming language as the course requires completion of assignments in either Ocatave or MATLAB. For the first programming assignment the course offers Octave/MATLAB script that steps you through the exercises. I am attempting to write a MATLAB function that will plot data for x and y variables, however, am continuing to run into the same error and am beginning to spin my wheels with finding a solution. I have a plotData.m file I am working off of and here is the code I have put together:
function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure
% PLOTDATA(x,y) plots the data points and gives the figure axes labels of
% population and profit.
figure; % open a new figure window
hold on;
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the
% "figure" and "plot" commands. Set the axes labels using
% the "xlabel" and "ylabel" commands. Assume the
% population and revenue data have been passed in
% as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
% appear as red crosses. Furthermore, you can make the
% markers larger by using plot(..., 'rx', 'MarkerSize', 10);
plot(x, y, 'rx');
xlabel('population (in tens of thousands)');
ylabel('profit (in $10,000s)');
hold off;
% ============================================================
end
Every time I run this script I receive an error stating 'Not enough input arguments. Error in plotData (line 19) plot(x, y, 'rx');'. This is a fairly ambiguous error message and I am not sure how to interpret what is wrong here. I don't see why I wouldn't have enough input arguments in this example as the function explicitly takes both x and y and uses each to plot data as defined by the function. Any help would be greatly appreciated.
I am using MATLAB version 9.6.0.1114505 (R2019a) Update 2

I was getting the same error on running 'plotData.m',
Please try running 'ex1.m' and not 'plotData.m'

Related

Problem with defining a transfer function for Bode plot in MATLAB

I am trying to tune a PID controller using Matlab(not Simulink because I am learning/uni coursework).
1. Summarize the problem:
So, I have a transfer function of a system for which there are phase margin requirement that needs to met
In order to find the phase advance part of the PID I need to solve a bunch of equations to plot a Bode plot using the variables calculated
2.Describe what I've tried
I tried to replace the tf([num],[den]) with real numbers but that is not feasible as it defeats the purpose of doing this, I want Matlab to calculate the GR and frequency and substitute that into the tf
Problem
Full_Code:
https://drive.google.com/file/d/1sWUnvvye_RBXGL8-nWq___3F5UDmDOoG/view?usp=sharing
Minimum reproducible code example:
clearvars;clc;clearAllMemoizedCaches;clear
syms s w
%--------------TF of the aircraft
G(s)= (160*(s+2.5)*(s+0.7))/((s^2+5*s+40)*(s^2+0.03*s+0.06));
k= 8; % selected k value range 4<k<8
Max_PA=asind((k-1)/(k+1)); % computes max phase advance
Centre_dB= 20*log10(sqrt(k)); % computing centre gain in dB
Poi= -120-Max_PA % looking for Point of interest(Poi)
tf_int= subs(G(s),1j*w); %intermediate transfer function
eqn= atan2d(imag(tf_int),real(tf_int))==Poi; % solve for w at Poi
% computing crossover freq(wc)
wc= vpasolve(eqn,w); % find exactly the wc at Poi
GR=20*log10(abs(subs(tf_int,w,wc))); % find the gain at at wc
Kpa= 10^((GR-Centre_dB)/20);
ti= 1/(sqrt(k)*wc); % computing Kpa and ti
num1= [Kpa*k*ti,Kpa];
den2= [ti,1];
PA= tf(num1,den2) %PA tf defined
Yo are trying to input non-numerical (symbolic numers) values into tf, which only accepts numerical arrays. You can convert them to that with double()
PA= tf(double(num1),double(den2)) %PA tf defined

Polyval issues after using polyint

Im trying to calculate the area of randomly generated graph, which is created from randomly generated x and y values and drawn using polyfit.
clear
clc
for i=1:8
x(i)= round((12+5).*rand - 5,0)
y(i)= round((7+6).*rand -6,0)
end
p=polyfit(x,y,5);
x1=-5:0.1:12;
y1=polyval(p,x1);
plot(x,y,'o')
hold on
plot(x1,y1)
y2=(x1)*0-5
plot(x1,y2)
hold off
syms x
S1=int(x.*0-5,x,-2,7)
pp=polyint(p,x)
S2=polyval(pp,-2)-polyval(pp,7)
S=S1+S2
However, I am getting this weird error that doesnt make any sense to me.
Undefined function 'filter' for input arguments of type 'sym'.
Error in polyval (line 56)
y = filter(1,[1 -x],p);
Why doesnt it allow me to use polyval after using polyint ? Its still a polynomial..
In other words. How could I change the end of the code to calculate the definite integral of the newly formed polynomial, which is always different

Plotting inside Matlab Function Block for real time signals in Simulink

I have a simulation running on Simulink and output signals change during simulation. I want to plot them at every step. What I can do is to use to Workspace blocks to transfer them to Matlab, but then I can only plot after the simulation finishes. I would like to plot the value at every instant of the simulation.
What I tried:
Create a figure in advance as: figure(1) and plot a static graph on it. Then I use
Matlab function inside Simulink :
function fcn(x,y)
coder.extrinsic('plot')
plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background')
Where x and y are my signals as input to matlab function block. However this results in plotting x and y in every timestep, but I would like to plot only the last value of the signal on the figure and delete the previous ones, in other words refresh the plot so that it is going to act as an animation. How can I achieve that? Thanks in advance
I think your code should work, with a few minor modifications:
I would do the following if I were you:
In the model callbacks, define your figure in the InitFcn callback:
fig_h = figure;
ax_h = axes;
set(ax_h,'Xlim',[0 12],'YLim',[0 12]) % or whatever axes limits you want
Then in your MATLAB Function block:
function fcn(x,y)
%#codegen
coder.extrinsic('plot')
plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background')
set(gca,'XLim',[0 12],'Ylim',[0 12]) % or whatever axes limits you want
You need little more elaborate function than just calling plot to animate your data. You should create a plot_fcn and make that function extrinsic. An example implementation of plot_fcn assuming scalar inputs with range 0 to 100 is
function plot_fcn(x,y)
persistent f h
if isempty(f)
f = figure;
h = plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background');
axis([0 100 0 100]);
axis manual
end
figure(f);
set(h, 'XData', x);
set(h, 'YData', y);
You can then call this function as
function fcn(x,y)
coder.extrinsic('plot_fcn')
plot_fcn(x,y);
Also checkout other questions regarding animation in MATLAB plots.

How to do MCMC simulation using Metropolis hasting algorithm in Matlab?

I am trying to simulate a distribution for parameter theta f= theta ^(z_f+n+alpha-1)*(1-theta)^(n+1-z_f-k+ beta-1), where all the parameter except for theta is know. I am using Metro polish hasting algorithm to do the MCMC simulation . My proposal density is a beta distribution with parameter alpha and beta. My code for the simulation are as follows. I am using a buitlin Matlab code called mhsample() for this purpose, How do I know if my code is working properly?
clear
clc
alpha=2;
beta=2;
z_f=1;
n=6;
k=5;
nsamples = 3000;
pdf= #(x) x^(z_f+n+alpha-1)*(1-x)^(n+1-z_f-k+beta-1); % here x acts as theta
proppdf= #(x,y) betapdf(x, alpha, beta);
proprnd =#(x) betarnd(alpha,beta,1);
smpl = mhsample(0.1,nsamples,'pdf',pdf,'proprnd',proprnd,'proppdf',proppdf);
I'm unsure of what you're asking when you say "how do I know if my code is working properly" -- I'm assuming it executes? But for a visual comparison of your function vs. the simulation, you can plot both the PDF and the data you got from mhsample as follows:
% i'm assuming you ran the code above so that smpl and #pdf are both defined...
fplot(pdf,[0 1]); % fplot takes your function and plots it between x-limit [0,1]
figure % new figure
hist(smpl,30); % 30 here is bin size, change it to your preference
Figure below:
the histogram of smpl's output on left, i.e., your simulation
the function pdf bounded in [0,1] on right for comparison to your simulation
This was just a wild guess because those two figures resemble each other and are also beta-distribution-esque.
If you want a more complex analysis than that, I'm afraid I'm not yet proficient in MCMC :)

plotting 2 variable of different size in matlab

Am trying to plot 2 variable of different size length in matlab GUI using push button,
but because the variables are of different length it will not work,is there a way i can make it to plot.
d= pdist([x,y,z],'euclidean') ; % value of my distance
dd= 1:10:d; % interval and end 'd' value
FSL=-120; %value of free space loss get from the GUI
DFSL= 1:10:FSL %interval and end at FSL value
plot(dd,DFSL)
The plot code didnt work coming back with an error "
Error using plot
Vectors must be the same lengths"
You can plot vectors of two different lengths, but not against each other. You have used the syntax
plot(x,y)
which means for every element in vector x, there should be a corresponding element in vector y. In your case, you do not have this, hence the error.
You can plot like this though:
plot(x)
figure;
plot(y)
If you are looking to plot them in a single plot, subplot will be useful.