Matlab cannot give the 3d surface plot of the following program.Matlab gives the value of all the variables in matrix form. But it cacnot plot the 3d graph using surf command. Why matlab cannot plot a 3d graph using 'surf' command in symbolic variable?? pls help me....
clear all
close all
clc
syms r
c=1;
for R=0.01:0.01:0.03
R1(c)=R;
j=1;
for l=0.3:0.01:0.4
l1(j)=l;
A=l*exp(-r^2);
B=int(A,0,inf);
B1(c,j)=B;
j=j+1;
end
c=c+1;
end
B1=real(B1)
surf(R1,l1,B1')
You just need to add these three lines after the last end:
B1=double(B1) % Converts from symbolic to numerical
[X ,Y]=meshgrid(R1,l1); % Creates a grid that is R1,l1 size, repeated
surf(R1,l1,B1') % plot!
Related
I have a vector with 1000 random numbers called v. I also have a vector, called x that represents the domain of which the numbers in v are generated, and another vector y that has the numbers of the cdf of the values in v. I know that I can do plot(x,y); and get a smooth function of the (non-empirical) cdf, and I also know that I can do cdfplot(v) to get a function of the empirical cdf.
My question is: How can I get these plots on the same set of axis?
Thank you for your help.
You could either generate data for an empirical cdf plot using ecdf or plot it directly with cdfplot like you mentioned. I would recommend using cdfplot since it sets up a few more things, such as a grid:
hFig = figure;
cdfplot(v);
hold all;
plot(x, y);
And as a bonus! Consider showing the X axis in logarithmic units, whichever reveals the data the best for you:
hAxes = get(hFig, 'CurrentAxes');
set(hAxes, 'XScale', 'log')
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.
Hello i am having some problems understading basic plotting in matlab.
I can understand why you would use a for loop when plotting data?
Can anybody explain this to me?
I am making a simple linear plot. Is there any reason this should be inside a loop
If you are making a simple plot there is virtually no reason to use a loop.
If you check doc plot you will find that plot can take some vectors as input, or even matrices for more interesting situations.
Example:
x=0:0.01:1;
y=sin(x);
plot(x,y)
No there is no need in Matlab to use a for loop for plotting. If you are looking for a simple linear plot your code could look like this:
x=1:100;
y=3*x+4;
plot(x,y)
As you see there is no for loop needed. Same goes for nearly all plots and visualization.
A possible reason to use a for loop to plot thing may be having several data to plot in a single matrix. Say you have two matrix Ax (MxN) and Ay (MxN) where N the length of each data and M is the amount of different data wanted to plot. For example like in this case N is 201 and M is 3:
% Create Ax and Ay
Ax=meshgrid(0:0.1:20,1:3);
Ay=zeros(size(Ax));
% Sinusoidals with different frequencies
for k=1:3
Ay(k,:)=sin(k.*Ax(k,:));
end
% create colours
colorVec = hsv(3);
% Plot
hold on
for k=1:3
plot(Ax(k,:),Ay(k,:),'Color',colorVec(k,:))
end
hold off
You get:
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.
I have 3D matrix of dimensions D x D x N. I want to create a dynamic heatmap to show how it's varying over N. Here's the MATLAB code I used to achieve this.
for n=1:N
heatmap(dynamicCov(:,:,n));
pause(0.5);
end
The issue with this code is that for each n, it opens a new figure window. I want it to be updated in the same Heatmap window. Is it possible to do that? Is there any other way to achieve this?
Thanks.
You need to use the undocumented 2nd input to HeatMap that indicates whether a plot should be created or not, and a few other Handle Graphics tricks to get the handle to the figure that is created. Something like
data = rand(20,20,10); % create test data
hmo = HeatMap(data(:,:,1),false); % create but do not plot
plot(hmo); % do the plot
allHFig = findall(0,'Type','figure'); % get handle to all open figures
hFig = allHFig(1); % we want the most recently created figure
for idx = 2:size(data,3)
hmo = HeatMap(data(:,:,idx),false); % create heatmap but do not plot
plot(hmo,hFig); % plot to our existing figure
pause(0.5);
end
I found a better and a lot simpler way of doing this. It uses built in imagesc() function instead of HeatMap() function from Bioinformatics toolbox. The code is as follows:
dynamicCov = rand(20,20,10); % create test data
N = size(dynamicCov,3);
for n=1:N
imagesc(dynamicCov(:,:,n));
colormap('copper');
colorbar;
pause(0.5);
end
Reference: http://buli.waw.pl/matlab-heatmap-colormap/