I can't understand what the problem is with this code. If anyone can help I will be thankful.
function [out] = detj(in1)
%DETJ Summary of this function goes here
%Detailed explanation goes here: N is 2*50 matrix and in1 is scaler
global N
out=(1/8)*(N(1,in1));
end
The error is :
Input argument "in1" is undefined.
Error in ==> detj at 7
out=(1/8)*(N(1,in1));
I defined N in another file
N=importdata('Nodes.txt'); %Matrix of nodes
No thing wrong with your code, you just used the function the wrong way.
To use a Function in Matlab you need to call it, NOT to run it directly.
This is your original function code
function [out] = detj(in1,in2,in3,in4)
%DETJ Summary of this function goes here
% Detailed explanation goes here %N is 2*50 Matrix
global N r s
out=zeros(2,2);
for m=1:2
for n=1:2
out(m,n)=(1/8)*(((N(1,in1)-N(1,in3))*(N(2,in2)-N(2,in4))-(N(2,in1)-N(2,in3))*(N(1,in2)-N(1,in4)))-r(1,m)*((N(1,in3)-N(1,in4))*(N(2,in1)-N(2,in2))-(N(2,in3)-N(2,in4))*(N(1,in1)-N(1,in2)))+s(1,n)*((N(1,in2)-N(1,in3))*(N(2,in1)-N(2,in4))-(N(2,in2)-N(2,in3))*(N(1,in1)-N(1,in4))));
end
end
end
To use your function I tried a simple code,
clc;clear;
global N
global r
global s
N=ones(2,50);
r=ones(2,2);
s=ones(2,2);
detj(1,2,3,4)
And I got answer as
ans =
0 0
0 0
So no thing wrong with your function, just you need to know how to use it.
Let me know if my answer is not clear else good luck.
Related
To solve one dimensional advection equation denoted by
u_t+u_x = 0, u=u(x,t), and i.c. u(x,0)= 1+H(x+1)+H(x-1)
using Lax Wanderoff method,
I need to write a Heaviside step function H(x) and it needs to be zero when x <= 0, 1 when x>0 . The problem is I also need to use that function writing H(x-t+1), H(x-t-1) as I will compare what I find by the exact solution:
u(x,t) =1 + H(x-t+1) -H(x-t-1)
Here, the "x" and "t" are vectors such that;
x=-5:0.05:5
t=0:0.05:1
I wrote the Heaviside step function as the following; however, I need it without the for loop.
L=length(x)
function H_X= heavisidefunc(x,L)
H_X=zeros(1,L);
for i= 1:L
if x(i)<= 0
H_X(i)=0;
else
H_X(i)=1;
end
end
end
I get "Dimensions must agree." error if I write
H_X3 = heavisidefunc(x-t+1,L);
H_X4 = heavisidefunc(x-t-1,L);
The Heavyside function is really easy to program in Matlab
Heavyside=#(x) x>= 0;
The easiest way to get rid of the dimensions must agree error is to transpose one of the vectors. This will cause Matlab to construct a matrix of length(x1) by length(x2)
Heavyside(x-t'+1);
I came up with a solution. My new Heaviside function is;
function H_X= heavisidefunc(x)
if x<= 0
H_X=0;
else
H_X=1;
end
end
The problem I had was because I was storing the output as a vector and it just complicated things.
Now,writing H(x-t+1), H(x-t-1) is easier. Just put "heavisidefunc(x(i)-t(j)-1)" and loop from 1 to the length of x and l in two loops. Thanks to everyone!
I have been trying to write a MATLAB code that can take in any function for optimization with as many variables as the user wants.
function Enter_Optimization_Code
clear all
clc
x=[];
U='';
n=input('Enter the number of variables= ')
U=input('Enter function with variables as x(1), x(2), x(3)..= ','s')
start=input('Enter coordinates of the starting point as [1,3,..]= ')
for i=1:n
x(i)=start(i)
end
int(U)
The code is asking the user to enter the number of variables they want and then the function they want to optimize (I didnt write the optimization code yet). For now, I want the code to plug in the value of the starting point into the function and spit out the answer.
e.g. I am entering a function that is x(1)+x(2) and start point as [1,2]. This should cause the code to do the calculation of 1+2=3 and print 3. This is what's happening instead:
Enter the number of variables= 2
n =
2
Enter function with variables as x(1), x(2), x(3)..= x(1)+x(2)
U =
x(1)+x(2)
Enter coordinates of the starting point as [1,3,..]= [1,2]
start =
1 2
x =
1
x =
1 2
Undefined function 'int' for input arguments of type 'char'.
Error in Enter_Optimization_Code (line 17)
Can anybody solve this?
If you are trying to integrate the function with the symbolic function int, you can't pass a string input to that function, it has to be a symbolic expression, check the documentation.
You probably need to do int(sym(U)) instead to convert the string U into a symbolic expression.
EDIT based on comments and using TroyHaskin's suggestion
Here's a version that should work. However, I'm not sure why this is a function, it should really be a script.
function Enter_Optimization_Code
% Not sure why you're using a function, this should really be a script
% clear all should only be used in scripts, not in functions
clc
% n=input('Enter the number of variables= '); not needed
U=input('Enter function with variables as x(1), x(2), x(3)..= ','s');
start=input('Enter coordinates of the starting point as [1,3,..]= ');
f = str2func(['#(x)',U]);
y = feval(f,start);
disp([U 'evaluated at x=([' num2str(start) ']) = ' num2str(y)])
If you want the user to enter the equation in this manner, you can convert the string to an anonymous function using str2func:
fun = str2func(['#(x)',U]);
This will work for function literals like x(1)-x(2)^x(3) and valid function calls my_opt(x).
I am doing some matlab work and I am stuck on this function and can't find what's wrong.
That is my function
function [e] = Ek(fk,m,n)
for i=8:m-7
for j=8:n-7
e(i,j)=some code here;
end
end
I am calling that function from an other file with this command
bla= Ek(array, m, n);
The error I am getting is
Error in Ek (line 2)
for i=8:m-7
Output argument "e" (and maybe others) not assigned during call to
"some path/Ek.m>Ek".
You should be pre-allocating e, for example
function [e] = Ek(fk,m,n)
e=zeros(m,n); %pre-allocate
for i=8:m-7
for j=8:n-7
e(i,j)=some code here;
end
end
You might have to check the values of the parameters m and n before the loop. They might be less than 15.
i want to understand what is a error in the following code?code is given below
function x0=secand2d(f,g,x,dx,tol)
% find solution of f(x,y)=0 and g(x,y)=0
for i=1:20
x0=x;
f0=[feval(f,x0),feval(g,x0)];
x0=x+[dx(1),0];
fx=[feval(f,x0),feval(g,x0)];
fx=(fx-f0)/dx(1);
x0=x+[0 dx(2)];
fy=[feval(f,x0),feval(g,x0)];
fy=(fy-f0)/dx(2);
A=[fx;fy]+eps;
x=x+dx;
dx=(-A\f0'-dx')';
if(norm(dx)<tol) ;return; end;
end;
disp(norm(dx));
pause;
end
which represents of two dimensional secant method,where function f and g is defined by following form
f=#(x,y) (x-sin(x+y)); and g=#(x,y) y-cos(x-y);
i have also checked and these to function works fine on it's parameters
f(3,3)
ans =
3.2794
and
g(1,1)
ans =
0
also
x=[0.9 0.9];
dx=[0.1 0.1];
tol=1e-5;
but following code generated such kind of errors
secand2d(f,g,x,dx,tol)
Error using #(x,y)(x-sin(x+y))
Not enough input arguments.
Error in secand2d (line 5)
f0=[feval(f,x0),feval(g,x0)];
please help me to clarify reasons of this errors
You are getting that error because you are passing only x0 to f here:
f0=[feval(f,x0),feval(g,x0)];
f and g are expecting two inputs. You can fix the error like this:
f0=[feval(f,x0,y0),feval(g,x0,y0)];
If there is not a particular reason you are using feval you can do it like this:
f0 = [f(x0, y0) g(x0, y0)];
I am getting the following error when I type in CalculateIntegral(2,5) into the MATLAB Command Window:
??? Error: File: CalculateIntegral.m Line: 2 Column: 1
Function definitions are not permitted at the prompt or in scripts.
I am not sure how to resolve this error. Thanks.
clear all;
function g = CalculateIntegral(s,N)
a=0;
b=1;
h=(b-a)/N;
x = 0:h:1;
g = ff(x).*exp(-s*x);
% compute the exact answer of the integral
exact_answer=antiderivative(b,s)-antiderivative(a,s);
% compute the composite trapezoid sum
If=0;
for i=1:(N-1)
If=If+g(i)*h;
end;
If=If+g(1)*h/2+g(N)*h/2;
If;
You can't have a clear all before your function definition (and you don't need one). Just remove that first line to make your code work. MATLAB functions need to be by themselves in their own file, named like the function (CalculateIntegral.m in your case).