Matlab function input arguments problems

1 view (last 30 days)
Hello.I would need some help with my university program. I needed to do a program on finite element method. I made it as a function with input arguments firstly, but now i want to input them from keyboard since i needed to add the program to a larger code with a menu in it and here's my problem. As soon as i did that the program went bananas and i get error at axis set and at second figure(2).
To make a general ideea, here's the code:
function []=Metoda_elementului_finit_cu_dreptunghiuri(a,b,c,d,N,M)
close all
clc
% a=input('a=');
% b=input('b=');
% d=input('c=');
% c=input('d=');
% N=input('N=');
% M=input('M=');
x=linspace(a,b,N+1);
y=linspace(c,d,M+1);
hx=(b-a)/N; %pasul pe axa Ox
hy=(d-c)/M; %pasul pe axa Oy
% crearea matricii nodurilor
Nod=zeros((N+1)*(M+1),3);
k1=0;
for j=1:M+1
for i=1:N+1
k=i+(N+1)*(j-1);
Nod(k,1)=x(i);
Nod(k,2)=y(j);
Nod(k,3)=0;
if((i~=1)&&(i~=N+1)&&(j~=1)&&(j~=M+1))
k1=k1+1;
Nod(k,3)=k1;
end
end
end
display(Nod);
%afisarea figurii pentru noduri
figure(1)
%construirea liniilor verticale
for i=1:N+1
plot(x(i)*ones(1,M+1),y)
hold on
%pause(0.2)
end
%construirea liniilor orizontale
for j=1:M+1
plot(x,y(j)*ones(1,N+1))
hold on
%pause(0.2)
end
%evidentierea nodurilor cu stelute sau cercuri
for k=1:(N+1)*(M+1)
if Nod(k,3)==0
plot (Nod(k,1),Nod(k,2),'*b')%noduri exterioare
hold on
else
plot(Nod(k,1),Nod(k,2),'or')%noduri interioare
hold on
end
axis([a-0.2,b+0.2,c-0.2,d+0.2])
end
%matricea dreptunghiurilor
Dr=zeros(N*M,4);
kT=0;
for j=1:M+1
for i=1:N+1
k=i+(N+1)*(j-1);%al catelea nod in numerotarea globala
if(i<=N)&&(j<=M)
kT=kT+1;
Dr(kT,1)=k;
Dr(kT,2)=k+N+1;
Dr(kT,3)=k+N+2;
Dr(kT,4)=k+1;
end
end
end
display(Dr);
%calculam MR si L
MR=zeros((N-1)*(M-1));
L=zeros((N-1)*(M-1),1);
nd=N*M;
%identificam coordonatele varfurilor dreptunghiului curent
for k=1:nd
xd=zeros(4,1);
yd=zeros(4,1);
for j=1:4
xd(j)=Nod(Dr(k,j),1);
yd(j)=Nod(Dr(k,j),2);
end
%calculam coeficientii functiei psi,a(j),b(j),c(j),d(j)
D=zeros(4);
for j=1:4
D(1:4,j)=[xd(j)*yd(j);xd(j);yd(j);1];
end
Dinv=inv(D);
A=Dinv(1:4,1);
B=Dinv(1:4,2);
C=Dinv(1:4,3);
%calculam matricea termenilor liberii Me si Le
Me=zeros(4);
Le=zeros(4,1);
ariaDr=(xd(3)- xd(1))*(yd(2)-yd(1));
for i=1:4
for j=1:4
R=((A(i)*A(j)*(yd(2)^2 + yd(1)*yd(2) +...
yd(1)^2 + xd(3)^2 + xd(3)*xd(1) + ...
xd(1)^2 )*1/3)+(A(i)*B(j)+A(j)*B(i))...
*(y(2)+y(1))+(A(i)*C(j)+A(j)*C(i))*(x(3)+...
x(1))*1/2 +B(i)*B(j)+C(i)*C(j))*ariaDr;
Me(i,j)=R;
end
Le(i)=(f(xd(i),yd(i))*ariaDr/4);
end
display(Me)
display(Le)
%se adauga Me si Le in MR si L(analog ca si in cazul triunghiurilor)
for i=1:4
for j=1:4
if((Nod(Dr(k,i),3)~=0) && (Nod(Dr(k,j),3)~=0))
MR(Nod(Dr(k,i),3),(Nod(Dr(k,j),3)))=...
MR(Nod(Dr(k,i),3),(Nod(Dr(k,j),3)))+Me(i,j);
end
if(Nod(Dr(k,i),3)~=0)
L(Nod(Dr(k,i),3))=L(Nod(Dr(k,i),3))+Le(i);
end
end
end
end
display(MR);
display(L);
%se rezolva sistemul MR*alpha=L
alpha=inv(MR)*L; %sau MR\L se foloseste pentru calcul mai rapid
display(alpha)
U1=zeros(N-1,M-1);
for i=1:N-1
for j=1:M-1
k=i+(N-1)*(j-1);
U1(i,j)=alpha(k);
end
end
U=zeros(N+1,M+1);
U(2:N,2:M)=U1;
display(U)
%solutie exacta
figure(2)
[X,Y]=meshgrid(a:0.1:b,c:0.1:d);
Z=2*sin(X).*sin(Y);
display(Z)
surf(X,Y,Z);
%solutie aproximativa
figure(3)
[X,Y]=meshgrid(a:hx:b,c:hy:d);
surf(X,Y,U');
function[z]=f(x,y)
z=2.*sin(x).*sin(y);
end
end
What i want to do exactly is remove all the input variable and uncomment the input ones below so i can add them from keyboard.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Apr 2017
"I made it as a function with input arguments firstly, but now i want to input them from keyboard since i needed to add the program to a larger code with a menu in it and here's my problem."
Taking them from the keyboard using input() will not be compatible with using graphic menus. Your GUI should extract the values for the parameters from the appropriate GUI elements and then should call upon your existing routine. For example,
a = str2double(handles.a);
b = str2double(handles.b);
c = str2double(handles.c);
d = str2double(handles.d);
N = str2double(handles.N);
M = str2double(handles.M);
Metoda_elementului_finit_cu_dreptunghiuri(a,b,c,d,N,M);
  6 Comments
Stephen23
Stephen23 on 30 Apr 2017
Edited: Stephen23 on 30 Apr 2017
The function addthem was an example. It is not a real function.
"I understand what you mean to do a function that stores variables beforehad"
No, there is no extra function storing the values beforehand. You only need your original function, which already is correct and works with input arguments.
The suggestion Walter Roberson made was to leave your original function unchanged (is would be very and design to replace the input arguments with input calls inside the function), something like this:
a = str2double(input('a=','s'));
b = str2double(input('b=','s'));
d = str2double(input('c=','s'));
c = str2double(input('d=','s'));
N = str2double(input('N=','s'));
M = str2double(input('M=','s'));
Metoda_elementului_finit_cu_dreptunghiuri(a,b,c,d,N,M)
It really is that easy.
Milu Mihai
Milu Mihai on 30 Apr 2017
Damn it i'm really dumb.Ok i made it work just made a separate function for the variables as you said. Thanks alot for all the help everyone!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!