Creat 3d plot- equation (2 paramenters) time dependent ? matlab - matlab

I have an equation
Z=aexp(kt)
Is there any way to "plot" the result "Z" in function of variation on the parameters "k" and "a" AND along time??
or to make a surface..Or I´ll always have to fixate on of the parameters?
cheers

Here is a basic example with color coding the time axis
clear;close all;clc;
t=0:0.02:0.2;
k = 0:10;
a = 0:100;
[x, y] = meshgrid(k, a);
figure;
colorList = colormap(jet);
hold on;
for ii=1:numel(t)
z=y.*exp(x.*t(ii));
h = surf(x, y, z);
set(h,'edgecolor','none','FaceColor',colorList(5*ii,:),'FaceAlpha',0.5);
end
hold off;
legend(cellstr(num2str(t', 't=%.2f')), 'location', 'northwest')
view([45 30]);
xlabel('k');
ylabel('a');
zlabel('Z');
and the result

Related

plotting a bullet-nose curves

I would like to plot this function of Two Variables you can find it here
$$z^2=t(t-i) \Longleftrightarrow x^2+y^2=4x^2y^2 \Longleftrightarrow y=\dfrac{\pm x}{\sqrt{4x^2-1}} \mbox{ with } |x|>\frac{1}{2}$$
would someone show me step by step how to plot this in matlab
is there any script or toolbox in http://www.mathworks.com/matlabcentral/fileexchange
which make plot of that kind of curves quickly
this is by geogebra
This is by wolframe
You can use symbolic variables with ezplot.
syms x y % makes symbolic variables
h1 = ezplot('-4*x^2*y^2+x^2+y^2'); % plots the equation
axis equal
set(h1, 'Color', 'k');
Or you can define a function,
f = #(x,y) -4.*x.^2.*y.^2+x.^2+y.^2;
h1 = ezplot(f);
set(h1, 'Color', 'k');
It won't be easy to have the axis in the middle, I hope it's not necessary to have that.
Edit
You can download oaxes here
syms x y
h1 = ezplot('-4*x^2*y^2+x^2+y^2');
axis equal
set(h1, 'Color', 'm');
oaxes('TickLength',[3 3],'Arrow','off','AxisLabelLocation','side',...
'LineWidth',1)
Edit
For 3D plot try this,
% First line provides a grid of X and Y varying over -5 to 5 with .5 as step-size
[X,Y] = meshgrid(-5:.5:5);
% instead of "=0", Z takes the values of the equation
Z = -4 .* X.^2 .* Y.^2 + X.^2 + Y.^2;
surf(X,Y,Z) % makes a 3D plot of X,Y,Z
You can also try contourf(X,Y,Z) for 2D plot.

matlab: how do I do animated plot in a figure of two subplot

There is example of the web that shows how to do animated plot in a single figure.
However, I want to do two subplots in a single figure, such that they will show animation in a first subplot, and then the animation ina second subplot.
Using 'figure(1)' or 'figure (2)' and 'hold on', I can do the animation plot as follows. However, How do I call the subplot to do the similiar things?
So the effect I am looking for is: 1) figure that is opened and has two subplot. 2) plot the animated curve in the 1st subplot, then plot the animated curve in the 2nd subplot. 3) I want to go back to the 1st subplot to plot more things, and also go to 2nd subplot to plot more things.
figure(1); hold on; x = 1:1000;
y = x.^2;
%// Plot starts here
figure,hold on
%// Set x and y limits of the plot
xlim([min(x(:)) max(x(:))])
ylim([min(y(:)) max(y(:))])
%// Plot point by point
for k = 1:numel(x)
plot(x(k),y(k),'-') %// Choose your own marker here
%// MATLAB pauses for 0.001 sec before moving on to execue the next
%%// instruction and thus creating animation effect
pause(0.001);
end
Just do the subplot's in the loop:
for k = 1:numel(x)
subplot(1,2,1)
plot(x(k),y(k),'-') %// Choose your own marker here
subplot(1,2,2)
plot(x(1:k),y(1:k))
%// MATLAB pauses for 0.001 sec before moving on to execue the next
%%// instruction and thus creating animation effect
pause(0.001);
end
% Easiest way
x = rand(1, 11); y = rand(1, 11);
z = rand(1, 11); a = rand(1, 11);
figure
for i = 1 : 10
subplot(211)
plot(x(i : i+1), y(i : i+1), '.-k');
hold on; % include this if you want to show plot history
subplot(212)
plot(z(i : i+1), a(i : i+1), '.-k');
drawnow;
pause(0.1);
end
% If you don't want to call "plot" interatively
x = rand(1, 11); y = rand(1, 11);
z = rand(1, 11); a = rand(1, 11);
figure
subplot(211)
p1 = plot(NaN, NaN, 'marker', 'o');
subplot(212)
p2 = plot(NaN, NaN, 'marker', 'd');
for i = 1 : 10
set(p1, 'xdata', x(i : i+1), 'ydata', y(i : i+1));
set(p2, 'xdata', z(i : i+1), 'ydata', a(i : i+1));
drawnow;
pause(0.1);
end
First define your plot as a construct, so p1 = plot(x,y). Then you set up your loop and in the loop your write
set(p1,'YData',y);
This will update the plot p1s YData which is y. If you want to see it in an animated form just add a pause(0.1) %seconds after the set.

Extract outline from MATLAB Isosurface

I have an isosurface I've plotted in MATLAB, e.g.:
And I'd like to extract the outline from this, for the given view settings that I currently have. The output I expect is like this (produced by GIMP):
Is there any way to programatically do this so I don't have to manually do it in GIMP?
Is this good enough? Edge detection taken from the bwboundaries documentation.
clear, close all
[x y z v] = flow;
figure(1)
p = patch(isosurface(x, y, z, v, -3));
set(p, 'FaceColor', 'red', 'EdgeColor', 'none');
daspect([1 1 1])
view(3)
grid off
axis off
print -dbmp test
I=imread('test.bmp');
G = im2bw(I, graythresh(I));
[B,L] = bwboundaries(~G,'noholes');
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), -boundary(:,1), 'k', 'LineWidth', 2)
hold on
end
hold off
Which results in:

Draw log graph curve on Matlab by clicking?

I'd like to draw a curve on an empty (semilog-y) graph by clicking the points I want it to run through, on the X-Y plane.
Is there a function for this?
edit: I'm trying to do this by obtaining the position of last pointer click -
axis([0 3000 0 1000]);
co=get(gcf, 'CurrentPoint');
It seems to return the cursor position at the time of execution, but it does not change later.
edit2: Here's what works for me. The actual drawing I can do by using the arrays of points collected.
clear
clc
h=plot(0);
grid on;
xlim([0 3000]);
ylim([0 1000]);
datacursormode on;
% Enlarge figure to full screen.
screenSize = get(0,'ScreenSize');
set(gcf, 'units','pixels','outerposition', screenSize);
hold on;
% Print the x,y coordinates - will be in plot coordinates
x=zeros(1,10); y=zeros(1,10);
for p=1:10;
[x(p),y(p)] = ginput(1) ;
% Mark where they clicked with a cross.
plot(x(p),y(p), 'r+', 'MarkerSize', 20, 'LineWidth', 3);
% Print coordinates on the plot.
label = sprintf('(%.1f, %.1f)', x(p), y(p));
text(x(p)+20, y(p), label);
end
Not really, but now there is:
function topLevel
%// parameters
xrange = [0 100];
yrange = [1e-4 1e4];
%// initialize figure, plot
figure, clf, hold on
plot(NaN, NaN);
axis([xrange yrange]);
set(gca, 'YScale', 'log')
t = text(sum(xrange)/2, sum(yrange)/2, ...
'<< Need at least 3 points >>',...
'HorizontalAlignment', 'center');
%// Main loop
xs = []; p = [];
ys = []; P = [];
while true
%// Get new user-input, and collect all of them in a list
[x,y] = ginput(1);
xs = [xs; x]; %#ok<AGROW>
ys = [ys; y]; %#ok<AGROW>
%// Plot the selected points
if ishandle(p)
delete(p); end
p = plot(xs, ys, 'rx');
axis([xrange yrange]);
%// Fit curve through user-injected points
if numel(xs) >= 3
if ishandle(t)
delete(t); end
%// Get parameters of best-fit in a least-squares sense
[A,B,C] = fitExponential(xs,ys);
%// Plot the new curve
xp = linspace(xrange(1), xrange(end), 100);
yp = A + B*exp(C*xp);
if ishandle(P)
delete(P); end
P = plot(xp,yp, 'b');
end
end
%// Fit a model of the form y = A + B·exp(C·x) to data [x,y]
function [A, B, C] = fitExponential(x,y)
options = optimset(...
'maxfunevals', inf);
A = fminsearch(#lsq, 0, options);
[~,B,C] = lsq(A);
function [val, B,C] = lsq(A)
params = [ones(size(x(:))) x(:)] \ log(abs(y-A));
B = exp(params(1));
C = params(2);
val = sum((y - A - B*exp(C*x)).^2);
end
end
end
Note that as always, fitting an exponential curve can be tricky; the square of the difference between model and data is exponentially much greater for higher data values than for lower data values, so there will be a strong bias to fit the higher values better than the lower ones.
I just assumed a simple model and used a simple solution, but this gives a biased curve which might not be "optimal" in the sense that you need it to be. Any decent solution really depends on what you want specifically, and I'll leave that up to you ^_^

Plotting of a expotential curve between an interval in MATLAB

I would like to plot an expotential curve between an interval based on a different multipler for each interval.
I have tried this:
%Plotting of h curve
function PlotQ(Time,h)
for i=2:size(Time,1)
for t=Time(i-1):0.1:Time(i)
plot([Time(i-1), Time(i)],exp(-h(i)*t))
hold on;
end
end
ymax = max();
xlim([1 max(Time)]);
ylim([-0.5 ymax+0.5]);
xlabel('Time')
ylabel('Rate')
end
The curve comes out like this:
Not sure what I am doing wrong.. Need some guidance..
New Edit:
T =[0;0.569444444444444;1.06666666666667;2.08611111111111;3.09722222222222;4.11111111111111;5.12500000000000;7.16111111111111;10.2000000000000;20.3444444444444;30.4944444444444];
%Plotting of h and Q
h = [0;0.0187;0.0194;0.0198;0.0215;0.0225;0.0241;0.0316;0.0379;0.0437;0.0452];
PlotQ(Time,h)
If I understood correctly, You are looking for something like this.
%Plotting of h curve
function PlotQ(Time,h)
for i=2:size(Time,1)
tVector=Time(i-1):0.1:Time(i);
sizetVector=length(tVector);
for t=2:sizetVector
plot([tVector(t-1), tVector(t)],[exp(-h(i)*tVector(t-1)),exp(-h(i)*tVector(t))]);
hold on
end
end
ymax = max();
xlim([1 max(Time)]);
ylim([-0.5 ymax+0.5]);
xlabel('Time')
ylabel('Rate')
end
I think, this is what you want:
function PlotQ(Time,h)
% Parameters
res = 0.1;
n = numel(h);
% Pre-allocate
y = cell(1,n);
t = cell(1,n);
% Calculate
for ii=2:n
t{ii} = Time(ii-1):res:Time(ii);
y{ii} = exp(-h(ii)*t{ii});
end
% Plot
t_ = [t{:}];
y_ = [y{:}];
figure;
plot(t_,y_);
axis([1 max(t_) -0.5 max(y_)+0.5]);
xlabel('Time');
ylabel('Rate');
end
It gives the following plot: