Matlab Countour Plot with trace - matlab

How can I plot the following data onto a contour instead?
close all;
clear all;
clc;
syms x
f=#(x) -100.*(x(2)-x(1).^2).^2+ (1-x(1)).^2;
x0=[-9, 1];
options=optimset('display', 'iter', 'TolX',1e-6);
%options = optimset('PlotFcns',#optimplotfval);
x=fminsearch(f, x0, options)
a= %need help pulling this from fminsearch [a,b,c]=fminsearch(f,x0,options] ???
b=%need help pulling this from fminsearch
fcontour(f, 'Fill', 'On');
hold on;
plot(a,b,'*-r');
xlim([-50 50]);
ylim([0 45]);plot
I would like to plot the point (a,b) for which the function is minimum on a contour tracing convergence towards the center.

Related

matlab saveas function results in empty plot

The following code creates figure(2), figure(3) and figure(5) but the saveas function results in an empty file in windows 10. I have to run the code a second time in order for the saveas function to work? How to fix?
clear; clc;
x=linspace(0,1,5);
x
f=#(x) x.^3
plot(x,f(x),'c'); hold on
figure(2)
saveas(figure(2),'f','emf')
x=linspace(0,1,5);
x
g=#(x) x.^2
plot(x,g(x),'m'); hold on
figure(3)
saveas(figure(3),'g','emf')
x=linspace(0,1,5);
x
f=#(x) x.^3;
g=#(x) x.^2;
plot(x,f(x),'r',x,g(x),'b'); hold on
figure(5)
saveas(figure(5),'fg2.emf')
The following code is similar and runs successfully the first time.
x=linspace(0,1,5);
x
f=#(x) x.^3;
plot(x,f(x),'r'); hold on;
figure(4)
g=#(x) x.^2;
plot(x,g(x),'b'); hold off
figure(4)
saveas(figure(4),'fg.emf')
I would like to plot f and g using
plot(x,f(x),'r',x,g(x),'b')
Why is this happening? Thank you in advance.
Sincerely, Mary
Your order is wrong, lets take the last example:
%first generate the output you want to plot
x=linspace(0,1,5);
f=#(x) x.^3;
g=#(x) x.^2;
%where do you want to plot it comes first
figure(4)
%and then the plot call
plot(x,f(x),'r');
hold on;
plot(x,g(x),'b');
hold off
%now you can save it
saveas(figure(4),'fg.emf')

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

create subplot with loop

i have following question and please help me to solve following task:
i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
but can i do linear form?like 1:100?or something like this ,more generally like this
n=100;
for i=1:n
subplot(1,n,i)
?
thanks very much
EDITED CODE
function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
[Pxx,f]=pburg(y,i,ndft,fs);
subplot(ndft,ndft,i);
plot(f,Pxx);
title(['order',num2str(i),'i']);
order=i;
end
end
picture :
i can't understand what happens
1-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
figure,
for k=1:4
subplot(4,1,k)
plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
xlim([0 40])
end
Output
2-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
colors=['r' 'g' ; 'y' 'k'];
figure,
for k1=1:2
for k2=1:2
subplot(2,2,(k1-1)*2+k2)
plot(t,y1,colors(k1,k2));
end
end
Output
Hopefully these demos would guide to you something meaningful for your case.
Yes, it is:
n=5;
for i=1:n
subplot(1,n,i)
end
gives
for pat=1: N % main loop
% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns
% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number
% Find the subplot number
sub_fig=mod(pat,s1*s2) % subplot number
% correct for corners
if(sub_fig==0)
sub_fig=s1*s2;
fig_num=fig_num-1;
end
% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;
plot(1,1) % plot something
end % of main loop

Plotting onto specified axes in Matlab GUIDE

I have the following code for a pushbutton using Matlab GUIDE.It is supposed to plot a rotating arrow which rotates to the angles specified by theta 1 and theta 2.
function start_pushbutton_callback_Callback(hObject, eventdata, handles)
% hObject handle to start_pushbutton_callback (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
theta1 = handles.xy_angle;
theta2 = handles.yz_angle;
delay = handles.speed;
axes(handles.axes_turntable_xy);
R= 1; %Radius of the turntable
E=[0;0;0];
C=[-2;0;0];% points on the X axis
D=[2;0;0]; % points on the X axis
G=[0;2;0]; % points on the Y axis
H=[0;-2;0];% points on the Y axis
for th =1:1:theta1
clf;
Radius =1;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal
L1= [R*cosd(th);R*sind(th);0];
drawvector(E,L1); % call the drawvector function, as previously
line([C(1),D(1)],[C(2),D(2)]);
line([G(1),H(1)],[G(2),H(2)]);
axis([-2 2 -2 2]);
grid on;
pause(delay);
end;
axes(handles.axes_turntable_yz) ;
R= 1; %Radius of the turntable
E=[0;0;0];
C=[-2;0;0];% points on the X axis
D=[2;0;0]; % points on the X axis
G=[0;2;0]; % points on the Y axis
H=[0;-2;0];% points on the Y axis
for th =1:1:theta2;
clf;
Radius = 1;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal
L1= [R*cosd(th);R*sind(th);0];
drawvector(E,L1); % call the drawvector function
line([C(1),D(1)],[C(2),D(2)]);
line([G(1),H(1)],[G(2),H(2)]);
axis([-2 2 -2 2]);
grid on;
pause(delay);
end
However, I am facing two problems with this code:
1) It simply plots onto a new matlab figure instead of plotting it on axes_turntable_xy-
2) It displays the following error afer executing till the line axes(handles.axes_turntable_yz). The error is as follows:
Error using axes
Invalid object handle
Error in turntable_interface_model>start_pushbutton_callback_Callback (line 235)
axes(handles.axes_turntable_yz) ;
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in turntable_interface_model (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)turntable_interface_model('start_pushbutton_callback_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Can anyone help with this?
Okay, I debugged the code and this was the problematic line in the above code:
for th =1:1:theta1
clf;
Radius =1;
[x,y,z] = cylinder(Radius,200);
The working code is:
for th1 =1:1:theta1
cla(handles.axes_turntable_xy); %clears the figure handles.axes_turntable_xy
Radius =1;
[x,y,z] = cylinder(Radius,200);
This clears the figure I am plotting in in Matlab, not some unspecified figure! Although it's still hard to understand why Matlab was plotting these commands in a new figure inspite of making axes.turntable_xy the current axes.

Drawing the derivative of a curve

I have a 3D curve in MATLAB, now I want to draw the derivative of that curve in another graph?
For example, for y = x2 the derivative w.r.t. x is y = 2x.
How can I do this ?
I do not understand the '3D' part. Why is y=x^2 a 3D curve?
But if you want to plot y=x^2 and its derivative on the same plot, use ezplot
clear all; close all;
syms x
y=x^2;
h=ezplot(y,[-6,6]);
set(h, 'Color', 'r');
hold on;
ezplot(diff(y,x),[-6,6]);