A Monster for Every Season: Summer 2
You can get A Monster for Every Season: Summer 2 now at Gumroad
Results 1 to 3 of 3
  1. - Top - End - #1
    Ogre in the Playground

    Join Date
    May 2007
    Sin City

    Default Coding Error Help

    Hey all,

    I received a code from my professor using a meshless mesh. It's a simple problem but there is a problem. It is getting close to what we need it to do but its not quite there.

    Spoiler: first run through

    Spoiler: second run through

    The picture shows a 2-D linear temperature plot. The problem being is that the contour plot should show straight lines. Even if I increase the number of points, the problem exists, though not as bad as above.

    The Code: Not very long. I cut out some of it that wasn't applicable. I almost want to say it seems like a boundary error somewhere.

    % Test meshless method for Steady State Heat Conduction Example

    % Clear workspace:
    close all
    clear all

    % Specify random or structured grids:
    random_grids = 1; % 1 = random, 2 = structured

    length_x = 1.0;
    length_y = 1.0;

    % Provide user defined values for structured grids (for grids =2):
    dx = 0.04; %1/25
    dy = 0.04; %1/25

    % Specify parameters for random grid generation (for grids = 1):
    num_avg_spanwise_grids = 11;

    temp_left_wall = 1;
    temp_right_wall = 2;

    if random_grids == 1

    dx_avg = length_x/(num_avg_spanwise_grids-1); %=0.025
    dy_avg = length_y/(num_avg_spanwise_grids-1); %=0.025

    dx = dx_avg; %=0.025
    dy = dy_avg; %=0.025

    % Define number of nodes
    num_x_nodes = length_x/dx+1; %=41 1x1 matrix
    num_y_nodes = length_y/dy+1; %=41 1x1 matrix
    total_nodes = num_x_nodes*num_y_nodes; %=1681 1x1 matrix
    grids = 1:total_nodes; %1x1681 matrix

    %Define 1681x1681 matrix and 1681x1 matrix
    Z = zeros(total_nodes,total_nodes); %1681x1681 matrix
    Z1 = zeros(total_nodes,1); %1681x1 matrix

    % Define shape parameter value based on given grid domain:
    %c = dx; % okay 0.025
    %c = 0.815*sqrt(dx^2+dy^2); % moderate 0.0281
    %c = 1.25*sqrt(dx^2+dy^2)/sqrt(total_nodes); % bad .0010779066786
    c = 2/sqrt(total_nodes); % great 0.04878

    % Preallocate matrices for speed:
    coord_x = Z1; %1681x1 matrix
    coord_y = Z1; %1681x1 matrix

    K = Z; %1681x1681 matrix
    F = Z1; %1681x1 matrix

    delxsq = Z; %1681x1681 matrix
    delysq = Z; %1681x1681 matrix
    delrsq = Z; %1681x1681 matrix

    total_shape_op = Z; %1681x1681 matrix
    total_shape_deriv_x_op = Z; %1681x1681 matrix
    total_shape_deriv_y_op = Z; %1681x1681 matrix
    total_shape_laplacian_op = Z; %1681x1681 matrix

    % Define all grid sets:
    all_points = 1:total_nodes; %1x1681 matrix
    bottom_wall = 1:num_x_nodes; %1x41 matrix
    top_wall = (num_y_nodes-1)*num_x_nodes+1:total_nodes; %1x41 matrix
    left_wall = 1:num_x_nodes:((num_y_nodes-1)*num_x_nodes+1); %1x41 matrix
    right_wall = num_x_nodes:num_x_nodes:total_nodes; %1x41 matrix
    interior_points = setdiff(all_points,[bottom_wall,top_wall,left_wall,right_wall]); %1x1521

    %left_wall=left_wall' %41x1 matrix
    %right_wall=right_wall' %41x1 matrix

    % Randomize all points:
    for L = all_points % 1:total_nodes 1x1681 matrix
    coord_x(L) = rand*length_x;
    coord_y(L) = rand*length_y;

    % Specify coordinate restrictions on walls (which will result in
    % correct edge and corner coordinates since points overlap properly)
    % Walls:
    coord_y(bottom_wall) = 0;
    coord_y(top_wall) = length_y;

    coord_x(left_wall) = 0;
    coord_x(right_wall) = length_x;

    % Plot grid coordinate locations:
    title('Grid coordinate locations')


    % Create completely sampled shape operator:
    for i = 1:total_nodes
    for j = 1:total_nodes
    delxsq(i,j) = (coord_x(i)-coord_x(j))^2;
    delysq(i,j) = (coord_y(i)-coord_y(j))^2;
    delrsq(i,j) = delxsq(i,j)+delysq(i,j);

    total_shape_op(i,j) = sqrt(delrsq(i,j)+c^2);
    total_shape_deriv_x_op(i,j) = (coord_x(i)-coord_x(j))/(delrsq(i,j)+c^2)^(1/2);
    total_shape_deriv_y_op(i,j) = (coord_y(i)-coord_y(j))/(delrsq(i,j)+c^2)^(1/2);
    total_shape_laplacian_op(i,j) = (delrsq(i,j)+2*c^2)/(delrsq(i,j)+c^2)^(3/2);

    % Apply interior domain conditions:
    K(interior_points,:) = total_shape_laplacian_op(interior_points,:);
    F(interior_points,1) = 0;

    % Apply boundary domain conditions:

    % Bottom wall:
    K(bottom_wall,:) = total_shape_deriv_y_op(bottom_wall,:);
    F(bottom_wall,1) = 0; % Value of derivative on bottom wall

    % Top wall:
    K(top_wall,:) = total_shape_deriv_y_op(top_wall,:);
    F(top_wall,1) = 0; % Value of derivative on top wall

    % Left wall:
    K(left_wall,:) = total_shape_op(left_wall,:);
    F(left_wall,1) = temp_left_wall; % Value of temperature on left wall

    % Right wall:
    K(right_wall,:) = total_shape_op(right_wall,:);
    F(right_wall,1) = temp_right_wall; % Value of temperature on right wall

    % Solve system of equations:
    U = K\F;

    % Derive actual temperature and temperature derivative values:
    T = total_shape_op*U;
    T_y_deriv = total_shape_deriv_y_op*U;
    T_laplacian = total_shape_laplacian_op*U;

    if random_grids == 1

    [X,Y] = meshgrid([0:(dx_avg):length_y],[0:(dy_avg):length_y]);
    T_plot = griddata(coord_x,coord_y,T,X,Y,'v4');
    F_2D_interp = TriScatteredInterp(coord_x, coord_y, T);
    T_plot = F_2D_interp(X,Y);


    % Plot results and boundary conditions to test accuracy
    title('Contour plot of Temperature')
    hold on
    hold off

    title('Surface plot of Temperature')
    Last edited by Deathslayer7; 2013-10-29 at 01:22 PM.
    Not wearing your seat belt? See you soon!
    Thanks to Kwarkpudding for this excellent avatar.

    Quote Originally Posted by Lady Tialait View Post
    This is perhaps the most amazing idea I have heard in eons. Thank you kind slayer of Death.

  2. - Top - End - #2
    Firbolg in the Playground

    Join Date
    Dec 2006
    Raleigh NC

    Default Re: Coding Error Help

    Hello, Deathslayer.

    I'm a little bit puzzled because I still don't quite understand what you're trying to do. If I understand this correctly, you're generating random points and then plotting lines through them, the whole producing a set of contours. Is that correct?

    Of course, random points don't line up in neat straight lines -- that is artificial order that must be imposed by the program. So I think you need a line of best fit , if you're looking to get straight lines out of this. Which is, the data won't line up naturally in straight lines and simply interpolating will give you the behavior you're seeing. If you want straight lines, you're going to need to first collect related points together into sets, then for each set generate a best-fit line to approximate the relationship between the different points in an individual set. One set, one line.

    Those are my suggestions. I'm sorry if it's not more helpful, but my time is limited, I'm afraid :).


    Brian P.
    "Every lie we tell incurs a debt to the truth. Sooner or later, that debt is paid."

    -Valery Legasov in Chernobyl

  3. - Top - End - #3
    Surgebinder in the Playground Moderator
    Douglas's Avatar

    Join Date
    Aug 2005
    Mountain View, CA

    Default Re: Coding Error Help

    Why are the lines supposed to be straight? If I'm remembering what a contour plot is correctly, straight lines have nothing to do with it.
    Like 4X (aka Civilization-like) gaming? Know programming? Interested in game development? Take a look.

    Avatar by Ceika.

    Saberhagen's Twelve Swords, some homebrew artifacts for 3.5 (please comment)
    Isstinen Tonche for ECL 74 playtesting.
    Team Solars: Powergaming beyond your wildest imagining, without infinite loops or epic. Yes, the DM asked for it.
    Arcane Swordsage: Making it actually work (homebrew)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts