How to generate a triangulation for FEM 1D - MATLAB? - matlab

I'm trying to plot Exact VS FEM solution for this equation:
with boundary condition:
I followed this code for example: Example Code
It's fine for its case, but in my case I don't know about how to get the triangulation (for my case) for this line:
x(1)=0; x(2)=0.1; x(3)=0.3; x(4)=0.333; x(5)=0.5; x(6)=0.75;x(7)=1;
Here is my code:
clear all; close all;
x(1)=0*pi; x(2)=0.1*pi; x(3)=0.3*pi; x(4)=0.333*pi; x(5)=0.5*pi; x(6)=0.75*pi;x(7)=1*pi;
U = fem1d(x);
x2 = 0:0.01:pi; k2 = length(x2);
for i=1:k2,
u_exact(i) = soln(x2(i));
u_fem(i) = fem_soln(x,U,x2(i));
end
error = norm(u_fem-u_exact,inf)
plot(x2,u_fem,':',x2,u_exact)
hold; plot(x,U,'o')
xlabel('x'); ylabel('u(x) & u_{fem}(x)');
title('Solid line: Exact solution, Dotted line: FEM solution')
figure(2); plot(x2,u_fem-u_exact); title('Error plot')
xlabel('x'); ylabel('u-u_{fem}'); title('Error plot')
How to generate the triangulation values? Does anyone know?
Thank you

Related

Image Transformation - Cartesian to Polar, and back again (MATLAB)

I have been using Peter Kovesi's MatLab functions for machine vision (which are outstanding if you aren't aware of them).
I have been transforming images to polar co-ordinates using the polar transform. The function from Peter Kovesi is named 'PolarTrans' and can be found here -
http://www.peterkovesi.com/matlabfns/#syntheticimages
The function beautifully transforms an images into polar co-ordinates. However, I would like the reverse to happen also. Peter Kovesi uses interp2 to transform images, but I can't seem to figure out how to reverse this transform. A requirement of interp2 is that it needs a meshgrid as input.
In short - can you help me reverse the transformation: polar to cartesian. I would like it be accordance with Peter's function - i.e. using the same parameters for coherence.
Dear Swjm,
I am posting my reply here because I do not have space in the comments section.
Firstly, thank you very much indeed for your reply. You have shown me how to invert interp2 - something I thought was impossible. This is a huge step forwards. However your code only maps a small segment of the image. Please see the demo code below to understand what I mean.
clc; clear all; close all;
gauss = fspecial('gauss',64,15);
gauss = uint8(mat2gray(gauss).*255);
[H,W] = size(gauss);
pim = polartrans(gauss,64,360);
cim = carttrans(pim,64,64);
subplot(2,2,1);
imagesc(gauss); colormap(jet);
axis off;
title('Image to be Transformed');
subplot(2,2,2);
imagesc(pim); colormap(jet);
axis off;
title('Polar Representation');
subplot(2,2,3);
imagesc(cim); colormap(jet);
axis off;
title('Back to Cartesian');
subplot(2,2,4);
diff = uint8(gauss) - uint8(cim);
imagesc(diff); colormap(jet);
axis off;
title('Difference Image');
I've had a look at Kovesi's code and this code should perform the reverse transformation. It assumes you used the 'full' shape and 'linear' map parameters in polartrans. Note that polar transforms generally lose resolution at low radial values (and gain resolution at high values), so it won't be lossless even if your polar image has the same dimensions as your original image.
function im = carttrans(pim, nrows, ncols, cx, cy)
[rad, theta] = size(pim); % Dimensions of polar image.
if nargin==3
cx = ncols/2 + .5; % Polar coordinate center, should match
cy = nrows/2 + .5; % polartrans. Defaults to same.
end
[X,Y] = meshgrid(1:ncols, 1:nrows);
[TH,R] = cart2pol(X-cx,Y-cy); % Polar coordinate arrays.
TH(TH<0) = TH(TH<0)+2*pi; % Put angles in range [0, 2*pi].
rmax = max(R(:)); % Max radius.
xi = TH * (theta+1) / 2*pi; % Query array for angles.
yi = R * rad / (rmax-1) + 1; % Query array for radius.
pim = [pim pim(:,1)]; % Add first col to end of polar image.
[pX,pY] = meshgrid(1:theta+1, 1:rad);
im = interp2(pX, pY, pim, xi, yi);

edge detection via wavelet transform(dwt2)

I want to detect edges of an image via dwt2. In fact I am going to simulate this article.
The first step of edge detection is based on replacing of all approximation coefficients with zeros. But when I replace the approximation coefficients with zero, the edges aren't similar to results seen in the article.
Here is my code:
clc;
clear all,close all
img=imread('2.png');
img=img(:,:,1);
imshow(img);
L = medfilt2(img,[3 3]);
L=im2double(L);
[A,H,V,D]=dwt2(L,'haar');
A=zeros(size(A));
Q1 = idwt2(A,H,V,D,'haar');
figure;
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(Q1);`
enter code here
clc;
clear all,close all
img=imread('2.png');
img=rgb2gray(img);
L = medfilt2(img,[3 3]);
t=graythresh(L);
b=im2bw(L,t);
[A,H,V,D]=dwt2(b,'haar');
A1=zeros(size(A));
Q1 = idwt2(A1,H,V,D,'haar');
figure;
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(Q1);

Change color map for ezsurf in Matlab

I am using a simple code to draw the 'Rosenbrock function'.
clear all;
clc;
syms x1 x2 st;
% Rosenbrock function
f = 100*(x2-x1^2)^2+(1-x1)^2;
% plot
figure(1);
ezsurfc(f,[-2,2,0,2]);
drawnow;
How can I change the colormap of the figure so that the ups/downs of the figure is more clear, for example around point (x1, x2) = (-1.2, 1). Following this link, 'colormap Lines' does the work somehow. But is there a better way to do this?
Use the contour3 command instead of ezsurfc.

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

how to write on each curve in a Matlab Plot

I have a set of data that I'm trying to analyse using the pwelch function of Matlab , my problem is that I have 13 curves and I get more , I want to label each curves, so i can deffirenciate between them, I tried using legend, but it didn't help .
here is my code and the result that I get:
clear, clc , clf;
M = 512;
k = 1
hold all;
grid on ;
for i =1:13
pause(2)
fileName =['M',int2str(i),'.wav']
[x,fs] = audioread(fileName);
[xPSD,f] = pwelch(x,hann(2048),1024,2048*4 ,fs);
plot(f,10*log10(xPSD))
text(7,30,fileName);
xlim([0 15e3]);
Leg{i} =['qho-',num2str(i)];
end
legend(Leg)
thanks for your help!