Problem with fplot - doesnt plot anything but when plotting the data individually it works
4 次查看(过去 30 天)
显示 更早的评论
Using this code, the aim is to calcuate the magnetic field produced by a solenoid within a 3D space. However, the fplot isnt working with my function, but when I substitute values into the fucntion, I get data points that make sense, and this is plotted using the for loop. Is anyone able to help fix this problem? As its an analytical solution, a function must be used to describe it. Thanks
The Code:
magnet.length = 0.25;
magnet.radius = 0.03;
magnet.position = [0, 0, 0];
magnet.I = 2.50E+01;
sphere.R = 0.25;
magnet.half_length = magnet.length / 2;
mu0 = 4*pi*1e-7;
syms x x1 % constants
f_magnet(x) = (mu0 * magnet.I * (magnet.radius^2)) / (2 * ((x-x1-magnet.position(1))^2 + magnet.radius^2)^(3/2)) * 1 / magnet.length; % Biot-Savart law
B_x_magnet(x) = int(f_magnet(x), x1, -magnet.half_length, magnet.half_length); % magnitude of B within the coil (uniform)
figure()
fplot(B_x_magnet(x), [-3*sphere.R 3*sphere.R])
hold off
figure()
for x = -3*sphere.R : 0.01 : 3*sphere.R
plot(x, B_x_magnet(x), 'o')
hold on
end
When plotting data using for loop:
When plotting using fplot:
2 个评论
Dyuman Joshi
2023-12-18
There are many undefined parameters in the code, so we can not run your code to reproduce the result you got.
采纳的回答
Dyuman Joshi
2023-12-18
You will have to define x as a real symbolic variable (or assume that the symbolic variable 'x' is a real number).
magnet.length = 0.25;
magnet.radius = 0.03;
magnet.position = [0, 0, 0];
magnet.I = 2.50E+01;
sphere.R = 0.25;
magnet.half_length = magnet.length / 2;
%% not defined, so value is assumed
mu0 = 1;
%% Define x as a real symbolic variable
syms x real
syms x1 % constants
f_magnet(x) = (mu0 * magnet.I * (magnet.radius^2)) / (2 * ((x-x1-magnet.position(1))^2 + magnet.radius^2)^(3/2)) * 1 / magnet.length % Biot-Savart law
B_x_magnet(x) = int(f_magnet(x), x1, -magnet.half_length, magnet.half_length) % magnitude of B within the coil (uniform)
figure()
fplot(B_x_magnet(x), [-3*sphere.R 3*sphere.R])
figure()
for x = -3*sphere.R : 0.01 : 3*sphere.R
plot(x, B_x_magnet(x), 'o')
hold on
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!