Polyval issues after using polyint - matlab

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

Related

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

not enough input arguments fminsearch

I'm trying to write a script in MATLAB that graphs a function in three dimensions using the mesh function and then finds the maximum of the surface. This is my code so far:
%% Aquifer, 3D maximum search
figure(2)
[X,Y] = meshgrid(-10:.5:10,-10:.5:10);
h = #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y));
mesh(h(X,Y)) %graph aquifer surface
aquamax = fminsearch(h,[-5;-5])
When I run the code I get this error:
Error using #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y))
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
I've read up on the fminsearch function but I'm not that familiar with it (still a bit of a noob at Matlab). Do I need to rework the code or is it just how I've input things into fminsearch?
Your h function requires 2 scalar inputs, but fminsearch only does one input, possibly a vector. Change h to h = #(x)-(1./(1+(x(1)-.25).^2+(x(2)-.5).^2+x(1)+x(1).*x(2))); and see if that works.

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.

plotting symfun and a self created function on same graph 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.

Matrix dimension error while calling mldivide in MATLAB

I am getting this error while running my code:
Error using ==> mldivide Matrix dimensions must agree.
Here is my code :
%make the plots of phase and group velocity vs discreteness of the grid
c=1;
a=input('Please enter the ratio cdt/dx : ')
figure(1)
R=2:40;
plot(R,phase_vel(R,a)/c)
xlabel('R=l/dx')
ylabel('u_phase/c')
%figure(2)
%plot(group_vel(R,a),R,0,40)
%xlabel('R=l/dx')
%ylabel('u_group/c')
and here are my functions :
function phase_velocity = phase_vel(R,a)
%numerical phase velocity of the discrete wave
c=1;
phase_velocity=(2*pi*c)/(R*knum(R,a));
end
function group_velocity =group_vel(R,a )
%numerical group velocity of the discrete wave
c=1;
group_velocity=(a*sin(knum(R,a)))/(sin(2*pi*a/R))
end
function knumber = knum(R,a)
%This is the k wave number
knumber=acos((1/a)^2*(cos(2*pi*a/R)-1)+1);
end
How can I resolve this error?
EDIT: I used . operator in every equation and i changed the limits of R=4:40
If your goal is to apply your formulas to each individual value in the vector R then you should be performing all of your computations using the element-wise arithmetic operators .*, ./, and .^ instead of the matrix operators *, /, and ^.
Your error is probably occurring in the first call to your function knum, specifically when you try to compute 2*pi*a/R. Since 2*pi*a is a single scalar value, you get an error when trying to perform matrix right division / using the row vector R. The really weird thing is the error message:
??? Error using ==> mldivide
Matrix dimensions must agree.
which implies you are using the matrix left division operator \, which you clearly aren't. I tested this in MATLAB R2010b and I get the same incorrect function name appearing in my message. I think this may just be a typo in the error message, and I've dropped a note to the MATLAB folks to take a look at it and clear it up.
I don't have the Symbolic Math Toolbox, but your problem seems to be that you are using plot, a function which can deal with arrays of numbers, and feeding it the result of a symbolic calculation. Have a look at the Matlab Help, where the Topic Creating Plots of Symbolic Functions suggests using ezplot(). Alternatively you need to evaluate your symbolic expression for certain input values to create an array of numbers that plot can deal with - but you can't use double() for that since it wouldn't know what numbers to plug into your variables.