Cant call function in script, function has switch case
显示 更早的评论
So here is my function, Im trying to do a switch case for a certain n-value
function [wi,na] = weight_natural(n_poly)
switch (n_poly)
case n_poly==1;
wi=2;
na=0;
case n_poly==2;
wi(1)=1;
wi(2)=1;
na(1)=-0.577350269190;
na(2)=0.577350269190;
case n_poly==3;
wi(1)=(5/9);
wi(2)=(8/9);
wi(3)=(5/9);
na(1)=-0.774596669241;
na(2)=0;
na(3)=0.774596669241;
case n_poly==4;
wi(1)=0.347854845137;
wi(2)=0.652145154863;
wi(3)=0.652145154863;
wi(4)=0.347854845137;
na(1)=-0.861136311594;
na(2)=-0.339981043585;
na(3)=-na(2);
na(4)=-na(1);
case n_poly==5;
wi(1)=0.236926885056;
wi(2)=0.478628670499;
wi(3)=0.568888888889;
wi(4)=wi(2);
wi(5)=wi(1);
na(1)= -0.906179845939;
na(2)= -0.538469310106;
na(3)= 0;
na(4)=-na(2);
na(5)-na(1);
end
end
My main script
clc
clear all
format short G
%Project one
read=dlmread('data.txt');
number_pts=read(1,1);% read total number of elements
starting_pt=read(1,2);% read starting global point
length=read(1,3);% total length of element
x_dist=read(2,:);% total element length
Ci=read(3,:);% coefficients
nodes=number_pts + 1;% number of node, between elements
n_poly = 3 ;
[wi,na] = weight_natural(n_poly)
j=1;
for k = -5: x_dist
x_value(1,j) = (((starting_pt + x_dist(j)))/2) + (((starting_pt - x_dist(j)))/2)*wi
j = j + 1;
end
回答(1 个)
Steven Lord
2019-2-16
0 个投票
Eliminate the "n_poly==" part of your case statements. MATLAB will perform the equality testing for you. See the "Compare Single Values" example on the documentation page for the switch keyword and use it as a model.
1 个评论
Walter Roberson
2019-2-16
This is the better solution. However, another potential solution is to change the
switch (n_poly)
to
switch true
and leave all of the n_poly== where they are.
switch true is obscure but legal. I do not recommend it, but it is possible.
类别
在 帮助中心 和 File Exchange 中查找有关 ARM Cortex-M Processors 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!