3d Fitting using a matlab function

2 views (last 30 days)
Hi~ Could you show me the answer to solve a 3d fitting problem? I want to calculate xyz using xyz_s and a fitting function of the Matlab.
Thank you.
WEST
% My Matlab code is started here close all;
clear all;
clc;
%
x = linspace(-1,1,100);
y = linspace(0,1,200);
z = linspace(-2,-1,300);
%
subplot(221); plot(x); title('x'); ylim([-3,3]);
subplot(222); plot(y); title('y'); ylim([-3,3]);
subplot(223); plot(z); title('z'); ylim([-3,3]);
%
temp = round(linspace(1,100,3+2));
temp(2:4)
x_3 = x(temp(2:4))
temp = round(linspace(1,200,5+2));
temp(2:6)
y_5 = y(temp(2:6))
%
temp = round(linspace(1,300,7+2));
temp(2:8)
z_7 = z(temp(2:8))
%
xyz_s = rand(5,3,7);
  2 Comments
Star Strider
Star Strider on 25 Oct 2014
Your x, y, and z vectors are all of different lengths, so you cannot fit any function to your data as you have presented them.
Seok Bae SEO
Seok Bae SEO on 27 Oct 2014
Edited: Seok Bae SEO on 27 Oct 2014
It is intended lengths. Anyway, thank you.

Sign in to comment.

Accepted Answer

Seok Bae SEO
Seok Bae SEO on 28 Oct 2014
Edited: Seok Bae SEO on 28 Oct 2014
%------ Answer by myself
[xq, yq, zq] = meshgrid(x, y, z);
%
xyz = interp3(x_3, y_5, z_7, xyz_s, xq, yq, zq, 'spline');
%
temp1 = zeros(1,3);
temp1(:) = xyz_s(1,:,1);
temp2 = zeros(1,100);
temp2(:) = xyz(1,:,1);
figure;
plot(x_3,temp1,'*',x,temp2,'-' );
title('x points (3) and fitting graph');
%
temp1 = zeros(1,5);
temp1(:) = xyz_s(:,1,1);
temp2 = zeros(1,200);
temp2(:) = xyz(:,1,1);
figure;
plot(y_5,temp1,'*',y,temp2,'-' );
title('y points (5) and fitting graph');
%
temp1 = zeros(1,7);
temp1(:) = xyz_s(1,1,:);
temp2 = zeros(1,300);
temp2(:) = xyz(1,1,:);
figure;
plot(z_7,temp1,'*',z,temp2,'-' );
title('z points (7) and fitting graph');
  1 Comment
Seok Bae SEO
Seok Bae SEO on 28 Oct 2014
Edited: Seok Bae SEO on 28 Oct 2014
I found a solution by myself.
Thank you for Star Strider, especially Image Analyst.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 25 Oct 2014
Are you trying to define a surface, like might be plotted in two and a half-D with surf()? If so, that's like a 2D image where you have an x (the columns) and a y (the rows) and for each (x,y) location you have a z value. That's a 2D situation. So if you want to interpolate, you can use interp2d() or griddedInterpolant(). Or if you have incomplete scattered data you can use scatteredInterpolant(). If you have lots of data and want to fit a smooth polynomial surface using regression, like a 2-D quadratic, then you can use John D'Errico's polyfitn() function: http://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
  3 Comments
Image Analyst
Image Analyst on 28 Oct 2014
If you have just one Z value for every unique x,y location, then that is a 2D situation, not a 3D situation.
Let's say you have topographic information about the height of the earth's surface, so every location on earth has an altitude - a Z value. And you could plot that as a "3D" (really 2.5d) rendering. Do you think that is a 3D situation? No it's not.
Let's say you have a volumetric CT image of the entire inside of a human body. Every x,y location has hundreds of z values. Every column through the body has a z coordinate and at every z coordinate you have a radiographic density value. Is that a 3D situation? Yes it is because every x,y,z location has a value - not just the surface Z value but every single z location has a density value associated with it.
I hope that explains it better.
Seok Bae SEO
Seok Bae SEO on 28 Oct 2014
Edited: Seok Bae SEO on 28 Oct 2014
Hi again 'Image Analyst'
Because input (the start) of my question is xyz_s = rand(3,5,7), every x, y location has 7 values. Is it 3-D ?
Could you check my code bellow?
Thank you so much for your comments.
WEST

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!