필터 지우기
필터 지우기

Editing a function and then using fzero

조회 수: 2 (최근 30일)
Tevel Hedi
Tevel Hedi 2022년 4월 8일
댓글: Tevel Hedi 2022년 4월 8일
I am trying to write a code that uses fzero on a function.
As I see it the only one way to edit and ran scripts on a function is by declaring it as syms type.
But syms does not work with fzero.
How should I approach it?
* I am trying to solve a nonlinear equation with two variables. So I have made two syms variables and wrote two function with those syms variables. So I get two syms functions. Then I use subs on two of the functions in order to have a one variable equation using those two functions (sub syms type to a scalar). I a adding the code.
clear
clc
%variables
u0 = 4*pi*10e-7;
Ms = 0.8;
gamma = 1;
Ku=1;
D = 0;
A = 1;
sinfei = sin(-pi/2);
delta=1;
syms d W
for m = 1:2
surfaceV(m) = (sin((pi/gamma)*W*m)/sinh((pi^2/gamma)*delta*m))^2*((1-exp(-(2*pi/gamma)*m*d))/m);
volumeV(m) = (sin((pi/gamma)*W*m)/cosh((pi^2/gamma)*delta*m))^2*((exp(-(2*pi/gamma)*m*d)+((2*pi/gamma)*m*d)-1)/m);
end
surfaceEnergy = (1/2)*u0*Ms^2*(1-2*W/gamma)^2+((2*pi*u0*Ms^2*delta^2)/(gamma*d))*sum(surfaceV);
volumeEnergy = ((2*pi*u0*Ms^2*delta^2*sinfei^2)/(gamma*d))*sum(surfaceV);
dW_surfaceEnergy = diff(surfaceEnergy,W);
dW_volumeEnergy = diff(volumeEnergy,W);
non_volumeORsurface_energy=-((2*delta*Ku+2*A/delta+pi*D*sinfei)/W^2); %Non volume nor surface energies for total energy solution
d_vector = double(linspace(100,200,3));
W_d=double(zeros(1,length(d_vector),'uint64'));
for m = 1:length(d_vector)
W_d(m)=double(fzero(subs(symfun(dW_surfaceEnergy+dW_volumeEnergy+non_volumeORsurface_energy,W),d,d_vector(m)),50));
end
  댓글 수: 2
Torsten
Torsten 2022년 4월 8일
As I see it the only one to edit and ran scripts on a function is by declaring it as sys type.
I don't know what you mean.
Jan
Jan 2022년 4월 8일
Please tell us more details.

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2022년 4월 8일
편집: Torsten 2022년 4월 8일
%variables
u0 = 4*pi*10e-7;
Ms = 0.8;
gamma = 1;
Ku=1;
D = 0;
A = 1;
sinfei = sin(-pi/2);
delta=1;
syms d W
for m = 1:2
surfaceV(m) = (sin((pi/gamma)*W*m)/sinh((pi^2/gamma)*delta*m))^2*((1-exp(-(2*pi/gamma)*m*d))/m);
volumeV(m) = (sin((pi/gamma)*W*m)/cosh((pi^2/gamma)*delta*m))^2*((exp(-(2*pi/gamma)*m*d)+((2*pi/gamma)*m*d)-1)/m);
end
surfaceEnergy = (1/2)*u0*Ms^2*(1-2*W/gamma)^2+((2*pi*u0*Ms^2*delta^2)/(gamma*d))*sum(surfaceV);
volumeEnergy = ((2*pi*u0*Ms^2*delta^2*sinfei^2)/(gamma*d))*sum(surfaceV);
dW_surfaceEnergy = diff(surfaceEnergy,W);
dW_volumeEnergy = diff(volumeEnergy,W);
non_volumeORsurface_energy=-((2*delta*Ku+2*A/delta+pi*D*sinfei)/W^2); %Non volume nor surface energies for total energy solution
expression = matlabFunction(dW_surfaceEnergy+dW_volumeEnergy+non_volumeORsurface_energy)
d_vector = linspace(100,200,30);
W_d=zeros(1,length(d_vector));
W0 = 1;
format long
for i=1:numel(d_vector)
d = d_vector(i);
W_d(i) = fzero(@(W)expression(W,d),W0);
error = expression(W_d(i),d)
W0 = W_d(i);
end
The solutions don't differ much - but they are solutions as can be seen by error.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 General Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by