Matlab surface plotting error for a specific function only - matlab

I have a small matlab script -
rho=1;
phi=0.5;
a=pi/4;
[x,y]=meshgrid(-1:1:1);
syms u;
Ex=abs(int(sqrt(cos(u))*exp(1i*rho*(cos(u-x.^2))),-a,a));
surf(x,y,Ex);
This throws the error
Error using matlab.graphics.chart.primitive.Surface/set
Invalid parameter/value pair arguments.
But there shouldn't be as Ex is a 3x3 matrix, so is the grid. Surface plotting should proceed smoothly.
But if I define a function that doesn't use the int() function, for example -
z=x.^2+y.^2;
surf(x,y,z);
Then there is no error.
Does it has something to do with syms variables?
Any help would be appreciated.

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

Matlab , Error while plotting Heaviside. (Vectors must be the same length.)

I am doing a problem on Hamming code in Matlab. I have generated a bit string of length 1190, for transmission. I am asked to display the string as a curve of step function.
After doing some researched i found that the Heaviside function can be used for display the bit string as unit step curve.
When I use the command fplot(heaviside(l_f),[0 ,10000]), to plot the curve, where l_f is the bit string of length 1190, I get this error
Error using fcnchk (line 106)
FUN must be a function, a valid string expression, or an inline
function object.
Error in fplot (line 60)
fun = fcnchk(fun);
Error in Untitled (line 88)
fplot(heaviside(l_f),[0 ,10000])
When i display using Plot, i.e plot(heaviside(l_f),[0 ,10000]), I get the error
Error using plot Vectors must be the same length.
Error in Untitled (line 88) plot(heaviside(l_f),[0 ,10000])
Anyway to plot the bit string as a curve of step function ?
fplot(heaviside(l_f),[0 ,10000]) won't plot since fplot requires a function as first parameter. But in here it is a matrix. So use plot instead. Next, dimension of heaviside(l_f) will be 1x1190 and dimension of [0 ,10000] is 1x2. So wont work since dimensions are different so use.
x=heaviside(l_f)
y=0:(10000+1)/length(l_f):10000;
plot(x,y);

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 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.