I have x y z data which looks like the following:
How do i create a surface across the lines using z values in matlab (interpolated surface)?
I tried this method but i am getting following error:
[fn,pn] = uigetfile('*.xyz','Open the file');
I = importdata([pn,fn], ',', 16);
x = I.data(:,1);
y = I.data(:,2);
z = I.data(:,3);
%%
spX = min(x):3:max(x);
spY = min(y):3:max(y);
[xC,yC] = meshgrid(spX,spY);
Vq = interp2(x,y,z,xC,yC);
Error using griddedInterpolant
The grid vectors must be strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 229)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 129)
F = makegriddedinterp({X, Y}, V, method,extrap);
Try griddata
spX = min(x):3:max(x);
spY = min(y):3:max(y);
[xC,yC] = meshgrid(spX,spY);
zC = griddata(x,y,z,xC,yC);
surf(xC,yC,zC)
Related
I have been trying to insert symbolic variable into a zero matrix.
syms k1 k2 k3 k4
global_k1 = zeros(4,4);
global_k2 = zeros(4,4);
global_k3 = zeros(4,4);
global_k4 = zeros(4,4);
global_k1(1,1) = k1;
global_k1(1,2) = -k1;
global_k1(2,1) = -k1;
global_k1(2,2) = k1;
global_k2(2,2) = k2;
global_k2(2,3) = -k2;
global_k2(3,2) = -k2;
global_k2(3,3) = k2;
global_k3(2,2) = k3;
global_k3(2,3) = -k3;
global_k3(3,2) = -k3;
global_k3(3,3) = k3;
global_k4(2,2) = k4;
global_k4(2,4) = -k4;
global_k4(4,2) = -k4;
global_k4(4,4) = k4;
k_stiff = global_k1+global_k2+global_k3+global_k4;
disp('The stiffness matrix is: ');
disp(k_stiff);
It displays the following error when I run the program:
The following error occurred converting from sym to double:
Unable to convert expression into double array.
Error in HW1_1 (line 9)
global_k1(1,1) = k1;
How should I add symbolic variables into a zero matrix?
Initialise them as symbolic matrices containing zeros if you later intend to replace its elements. i.e
global_k1 = sym(zeros(4,4));
It would be better to create a single 3D matrix instead of dynamic global_k1, global_k2, global_k3 and global_k4 matrices i.e.
global_k = sym(zeros(4,4,4));
Now global_k(:,:,1) will be your global_k1 and so on.
I want to do something like this:
syms x1 x2 h11 h12 h21 h22
x = [x1 x2]
matrix = [h11 h12; h21 h22]
And then create my function as:
f(x, matrix) = ((2*matrix)^(-1/2))*(x');
But I must be doing something wrong because I get:
Error using sym/cat>checkDimensions (line 75) CAT arguments dimensions
are not consistent.
Error in sym/cat>catMany (line 38) [resz, ranges] =
checkDimensions(sz,dim);
Error in sym/cat (line 27)
ySym = catMany(dim, strs);
Error in sym/horzcat (line 19)
ySym = cat(2,args{:});
Error in sym/subsasgn (line 1639)
C = symfun(B,[inds{:}]);
The error is in your function definition. See this link for how to correctly declare a function in MATLAB.
Put this in a separate file named "your_function_name.m"
function result = your_function_name(x, matrix)
result = ((2*matrix)^(-1/2))*(x');
Then you can call your function like this:
x = [x1 x2];
matrix = [h11 h12; h21 h22];
your_function_name(x, matrix);
Or you can use an anonymous function.
x = [x1 x2];
matrix = [h11 h12; h21 h22];
f = #(x, m) ((2*m)^(-1/2))*(x');
f(x, matrix);
I'm trying to calculate some functions in matlab and I'm getting this error:
Error using *
Inner matrix dimensions must agree.
Error in set1 (line 11)
x = (Ac + m)*cos(2*pi*fc*t);
but I don't use any kind of matrix in my code. What is the problem about?
Here is my code:
fs = 10000;
Ts = 1/fs;
t = (0:Ts:10);
m = cos(2*pi*t);
plot(t,m);
figure;
Ac = 2;
fc = 500;
x = (Ac + m)*cos(2*pi*fc*t);
plot(t,x);
figure;
Try elementwise multiplication by adding a dot before *:
x = (Ac + m).*cos(2*pi*fc*t);
I am trying to interpolate the following function using the MATLAB function spline,
at equidistant points xi = i./n, i = 0,1,...,n, and for n = 2^j, j = 4,5,...,14.
For each calculation, I record the maximum error at the points x = 0:0.001:1 and plot these errors against n using a loglog plot.
Below is the code,
index=1
for j = 4:1:14;
n = 2^j;
i = 0:1:n;
xi = i./n;
yi = ((exp(3*xi))*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
x = 0:.001:1;
ye = ((exp(3*x))*sin(200*x.^2)) ./(1+20*x.^2);
yp = spline(x,xi,yi);
err = ye - yp;
merr(index) = max(err);
index = index+1;
end
n1 = 10:10:170;
loglog(n1, merr,'.')
xlabel('n');
ylabel('errors');
title('Cubic Splines');
but when I run the code, I got the following error:
Error using * Inner matrix dimensions must agree.
Error in (line 9) yi = ((exp(3*xi))sin(200.(xi.^2)))
./(1+20.*(xi.^2));
I'm just starting learning MatLab, can anyone help?
You want element-wise multiplication (.*) for the following part of code:
yi = ((exp(3*xi))*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
should be
yi = ((exp(3*xi)).*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
The same issue is present for the computation of ye.
When you use mtimes (*), MATLAB tries to do matrix multiplication, which in your case (1-by-n times 1-by-n) is invalid.
Then you will run into a problem with your spline command. Change it to yp = spline(xi,yi,x); so that the values at which you want to interploate (x) are the last argument.
I want to solve equations in matlab, eg.
100+a/2=173*cos(b)
sqrt(3)*a/2=173*sin(b)
and the code would be:
[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
However, if I want to take 100 as a variable, like
for k=1:100
[a,b]=solve('k+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end
There would be an error, how to make it?
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
for i=451:550
for j=451:550
alpha=(1145-i)*degree;
beta=(1145-j)*degree;
x_2=p/cos(alpha)*tan(beta);
y_2=0;
z_2=p*tan(alpha);
syms x y z x_1 x_2 y_1 y_2 z_1 z_2 a b
eq = [(x-x_1)*(y2-y_1)-(x_2-x_1)*(y-y_1),(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1), b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];
sol = solve(eq(1),x,eq(2),y, eq(3),z);
sol.x
sol.y
sol.z
end
end
I got the expression value, how do I get the numeric value of x,y,z?
[['x(1)=';'x(2)='],num2str(double(sol.x))]
not work ,shows
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> sym.sym>sym.double at 927
Xstr = mupadmex('mllib::double', S.s, 0);
Error in ==> f2 at 38
[['x(1)=';'x(2)='],num2str(double(sol.x))]
If you have access to the Symbolic Toolkit then you do the following:
syms a b k
eq = [k+a/2-173*cos(b), sqrt(3)*a/2-173*sin(b)];
sol = solve(eq(1),a,eq(2),b);
sol.a = simplify(sol.a);
sol.b = simplify(sol.b);
% There are two solutions for 'a' and 'b'
% check residuals for example k=20
subs(subs(eq,{a,b},{sol.a(1),sol.b(1)}),k,20)
% ans = 0.2e-13
subs(subs(eq,{a,b},{sol.a(2),sol.b(2)}),k,20)
% ans = 0.2e-13
Edit 1
Based on new code by OP the matlab script to solve this is:
clear all
clc
syms alpha beta
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
x_2 = p/cos(alpha)*tan(beta);
y_2 = 0;
z_2 = p*tan(alpha);
syms x y z
eq = [(x-x_1)*(y_2-y_1)-(x_2-x_1)*(y-y_1);...
(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1); ...
b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];
sol = solve(eq(1),x,eq(2),y,eq(3),z);
sol.x = simplify(sol.x);
sol.y = simplify(sol.y);
sol.z = simplify(sol.z);
pt_1 = [sol.x(1);sol.y(1);sol.z(1)] % First Solution Point
pt_2 = [sol.x(2);sol.y(2);sol.z(2)] % Second Solution Point
x = zeros(100,100);
y = zeros(100,100);
z = zeros(100,100);
for i=451:550
disp(['i=',num2str(i)])
for j=451:550
res = double(subs(pt_1,{alpha,beta},{(1145-i)*degree,(1145-j)*degree}));
x(i-450, j-450) = res(1);
y(i-450, j-450) = res(2);
z(i-450, j-450) = res(3);
end
end
disp('x=');
disp(x);
disp('y=');
disp(x);
disp('z=');
disp(x);
I would try
for i=1:100
k=num2str(i)
[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end
and then solve the equation