Verify this matlab code - matlab

I want to plot the fractal generated by the functions:
x(k+1)=y(k)(1+sin(0.7x(k))-1.2+sqrt(|x|))
y(k+1)=0.21-x(k)
with the initial conditions y(0)=x(0)=0, then I did the following code.
x=zeros(100);
y=zeros(100);
x(1)=0;
y(1)=0;
for k=1:100
x(k+2)=y(k+1)(1+sin(0.7x(k+1))-1.2+sqrt(abs(x+1)))
y(k+2)=0.21-x(k+1)
end
plot(x,y,'.')
So, Am I right?, or How can I fix it if I am wrong?, Well the thing is that I want to have a fractal,but I have run this out and it gave me only isolated points, the requirement is not to join them, therefore I want to know of this code has the right sintaxis and I it works well, so the thing is that if it is doing the right thing ;)
Thanks a lot for your help.
Edition
x=zeros(100);
y=zeros(100);
x(1)=0;
y(1)=0;
for k=0:100
x(k+2)=y(k+1)*(1+sin(0.7*x(k+1))-1.2*sqrt(abs(x(k+1))));
y(k+2)=0.21-x(k+1);
end
plot(x,y,'.')

Using the code you provided, I added the line print("MyPNG.png", "-dpng") and ran the code from Saturn Fiddle.
x=zeros(100);
y=zeros(100);
x(1)=0;
y(1)=0;
for k=0:100
x(k+2)=y(k+1)*(1+sin(0.7*x(k+1))-1.2*sqrt(abs(x(k+1))));
y(k+2)=0.21-x(k+1);
end
plot(x,y,'.')
print("MyPNG.png", "-dpng")

Related

Problem with implementing eigenface method based on a sample source code

I am imitating implementation of eigenface method for face recognition in MATLAB. I am using the sample code which is briefly discussed in the following video:
https://www.youtube.com/watch?v=V-Miq_k3BJk
The sample code is attached in the following:
loaded_Image=load_img();
random_Index=round(400*rand(1,1));
random_Image=loaded_Image(:,random_Index);
rest_of_the_images=loaded_Image(:,[1:random_Index-1 random_Index+1:end]);
image_Signature=20;
white_Image=uint8(ones(1,size(rest_of_the_images,2)));
mean_value=uint8(mean(rest_of_the_images,2));
mean_Removed=bsxfun(#minus,rest_of_the_images,uint8(single(mean_value)*single(white_Image)));
L=single(mean_Removed)'*single(mean_Removed);
[V,D]=eig(L);
V=single(mean_Removed)*V;
V=V(:,end:-1:end-(image_Signature-1));
all_image_Signature=zeros(size(rest_of_the_images,2),image_Signature);
for i=1:size(rest_of_the_images,2);
all_image_Signature(i,:)=single(mean_Removed(:,i))'*V;
end
subplot(121);
imshow(reshape(random_Image,112,92));
title('Looking for this Face','FontWeight','bold','Fontsize',16,'color','red');
subplot(122);
p=random_Image-mean_value;
s=single(p)'*V;
z=[];
for i=1:size(rest_of_the_images,2)
z=[z,norm(all_image_Signature(i,:)-s,2)];
if(rem(i,20)==0),imshow(reshape(rest_of_the_images(:,i),112,92)),end;
drawnow;
end
[a,i]=min(z);
subplot(122);
imshow(reshape(rest_of_the_images(:,i),112,92));
title('Recognition Completed','FontWeight','bold','Fontsize',16,'color','red');
I do not know why we use "image_signature" variable in our code. I can not understand what value for this variable is required. can anyone help?
Best regards,
Thank you

What does imresizemex do in Matlab imresize function?

I was trying to create an image resize code using C to do exactly what imresize.m in Matlab does. I stuck at the line calling imresizemex in imresize.m. It seems that imresizemex is a compiled machine code that can only be called in Matlab (I found it as imresizemex.mexw64 file in a Matlab private folder, no source code available). I also tried to call it in C, but failed. Does anyone know where to find the source code for imresizemex? Thanks a lot!
I think I figured it out ^ ^. It does the weighted multiplication and sum part of the cubic convolution interpolation. Here's my Matlab code replacing imresizemex. Although almost 6 seconds slower, it produces exactly the same result.
function outimg=reducesize(inimg, weights,indices,dim)
% reduce first dimension
reduce1=zeros(dim(1),size(inimg,2));
weight1=weights{1};
index1=indices{1};
for i=1:size(inimg,2)
for j=1:dim(1)
w11=weight1(j,:);
ind11=index1(j,:);
B=double(inimg(ind11,i));
v=w11.*B';
reduce1(j,i)=sum(v);
end
end
% reduce second dimension
reduce2=zeros(dim(1),dim(2));
weight2=weights{2};
index2=indices{2};
for i=1:dim(1)
for j=1:dim(2)
w22=weight2(j,:);
ind22=index2(j,:);
B=reduce1(i,ind22);
v=w22.*B;
reduce2(i,j)=sum(v);
end
end
outimg=round(reduce2);

Failed to plot simple graph in Octave

I want to draw a line on a graph to find the intersection point with another line. However, there's no response after I executed the script below. May I know what is the problem and how can I solve it?
x=1:2^20;
y2=2^24;
plot(x,y2);
Thanks!
What you want is to plot a line on 2^24. However, there are too many points for you computer probably, and you run out of memory
I am guessing that you'll need to plot your other inequality as well.
Something like
x=1:100:2^20;
% As Zoran and others suggested, You may not want all the points!
% It is too much memory
y2=2^24*ones(size(x)); % This ones is optional, but its good to know what you are doing (personal opinion)
plot(x,y2);
hold on
y1=(x+1).*log(x);
plot(x,y1);
However, you are still not there!
Another solution, which does not rely on plotting:
>> f = #(x) (x+1)*log(x)-2^24;
>> soln = fzero(f,1e6)
soln = 1.1987e+006
>> f(soln)
ans = 3.7253e-009
So your intersection point is at 1.1987e6.
Apparently,you have too many points for x, 2^20
Have to wait program to calculate, or plot,for example, every 100th point
This solution works for Matlab
x=1:100:2^20;
y2=2^2;
plot(x,y2,'o');
There is one more and maybe a bit smarter way: if you want to solve ((k+1)(ln k)<2^24) as you've commented above, use fsolve function to get just solution of an equation(!). Then use that solution to specify the area of your interest, so you won't have to plot the domain of 2^20.
(All functions are continuous so you don't have to worry about any wild singularities. Just examine the neigborhood of ks for which (k+1)(ln k)-2^24=0.)

How to correct the code to get the graphs of ALL functions together in one graph?

This is related to an old question of mine. I was trying to plot the graphs of the functions f_k(t)=t+k for 1<=k<=10 in the , but whenever I write the following code
syms t;
k=1;
while k<=6;
f_k(t)=k+t;
ezplot(f_k,[0,5]);
k=k+1;
end;
it runs perfectly and gives me the graph of f_6(t)=t+6 only. I checked with replacing 6 by 10 and the same thing happens. I checked the code, and couldn't detect any logical error. I also tried 1)using #(t) and function command in the while loop and 2)also using for loop, but couldn't plot because there were other errors.
a)What exactly is wrong with my code?
b)How can I fix this without minimal correction?
Each time you call ezplots, the current figure is overwritten. Call hold on so the figure is not overwritten and hold off after the code to allow overwriting again.
Further I would recommend to use a for loop instead of while.
syms t;
hold on;
for k=1:6
f_k(t)=k+t;
ezplot(f_k,[0,5]);
end
hold off

Need help understanding several MATLAB statement

All are from this post.
What does these statement mean:
error(nargchk(5, 6, nargin));
plot(p(:,1), p(:,2), '.-'), axis equal
And what's this kinda syntax which I haven't quite often seen:
if nargin<6, steps = 36; end
I hope I don't come over as too unhelpful, but have you tried:
using Matlab's built-in help facilities to discover for yourself what the various statements mean ? or
running any of the code ?
All the statements are Matlab intrinsics and well documented. The use of , to separate statements on the same line is a bit unusual (I, for one, would usually put the statements on different lines in your examples), but not incorrect
Take a look at
help plot
help axis
help nargin