plotting symfun and a self created function on same graph matlab - matlab

i am trying to plot two function in matlab, the first one is of kinf symfun:
p = symfun(0, [m]);
p(m) = p(m)+Ck(k-3)*exp(m*(k-3)*complex(0, 2*pi/25));
here Ck is another symfun and k is a variable i pre-defined.
i want to plot it in the same graph with a function i created using the function mode:
function [x1] = xt_otot_q3( t)...
i cant make the xt_otot_q3 function a symfun because it involves if statements.
- i tried to create 2 vectors sampling the two functions and plotting them together with the plot function but for some reason the 'p' function vectors gets preatty grotesque giving me wierd output...
- i tried plotting them both using ezplot function but for some reason the sampled vector i got form xt_otot_q3 shows only as a straight line at 0.
any ideas how i should plot them together? to plot the xt_otot_q3 function i must create a vector if i try to plot it directly using ezplot it gives me the following eror:
>> ezplot(xt_otot_q3, [-10 10])
Error using xt_otot_q3 (line 2)
Not enough input arguments.
thanks in advance.

If I am understanding it properly, you have two functions p, and xt_otot_q3. You want to plot them together.
syms t;
func1 = xt_otot_q3(t);
ezplot(func1, [-10 10]);
# retain current graph, for new graph
hold on;
# symbolic function p
ezplot(p, [-10 10]);
I hope it helps.

Related

Error when plotting in Octave

In Octave 4.0.2 I have defined a function S as follows:
S = #(x) (Y(k)+((Y(k+1)-Y(k))/h(k)-(2*M(k)+M(k+1))*h(k)/6)*(x-X(k))+M(k)*(k-X(k))^2/2+(M(k+1)-M(k))*(x-X(k))^3/(6*h(k)))
When I call it to evaluate a number in the interval [X(k), X(k+1)] I get the result I expect, but when I try plotting it with the command:
fplot(S, [X(k), X(k+1)]); hold on;
I get the error "error: for A^b, A must be a square matrix. Use .^ for elementwise power."
What is going on?
Alright, I figured out that when passing a function to fplot it must be able to take a vector of inputs and return a vector of outputs.

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

Ezplot cosine function in Matlab

I'm trying to ezplot this function(f(x)= e^-2t cost t=[-20,20]), and I guess i'm missing the syntax or something.
t=[-20:20]
x= e^-2*t,cos(t)
ezplot(t,x)
but it bringing out an error
you're mixing between x-y plot and function plot, plus you have several syntax errors.
First, t is a 41 elements vector between -21 and 21.
Second, unless you predefined a variable e then e^(-2*t) will give you Undefined function or variable 'e'. error. for exponential function just use exp(-2*t).
Third, assume you fixed the syntax errors such that x = exp(-2*t).*cos(t);, then x is also a 41 elements vector, and therefore you can simply plot it using plot(t,x). ezplot (or fplot in newer versions) is used for plotting functions (rather than vectors). If you want to plot the function in the [-21,21] interval do something like:
f = #(t) exp(-2*t).*cos(t); % this is a function handle
ezplot(f,[-20 20]) % use ezplot with function handle and t interval

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.

Matlab, figures and for loops

I am trying to plot the following simple function; $y=A.*x$ with different A parameter values i.e. A=0,1,2,3 all on the same figure. I know how to plot simple functions i.e. $y=x$ by setting up x as a linspace vector so defining x=linspace(0,10,100); and I know that one can use the hold command.
I thought that one could simply use a for loop, but the problem then is getting a plot of all the permutations on one figure, i.e. I want a plot of y=t,2*t,3*t,4*t on the same figure. My attempt is as follows:
x=linspace(0,10,100);
%Simple example
Y=x;
figure;
plot(Y);
%Extension
B=3;
F=B*x;
figure;
plot(F);
%Attempt a for loop
for A= [0,1,2,3]
G=A*x;
end
figure;
plot(G);
This is how I would plot your for loop example:
figure;
hold all;
for A=[0,1,2,3]
G=A*x;
plot(G);
end
figure creates a new figure. hold all means that subsequent plots will appear on the same figure (hold all will use different colours for each plot as opposed to hold on). Then we plot each iteration of G within the loop.
You can also do it without the loop. As with most things in Matlab, removing the loop should give improved performance.
figure;
A=[0,1,2,3];
G=x'*A;
plot(G);
G is the outer product of the two vectors x and A (with x having been transposed into a column vector). plot is used to plot the columns of the 100x4 matrix G.